gopgext/http

import "gopgext/http"

Package http is an HTTP(S) client for extensions built with gopgext, on libcurl (TinyGo has no net/http here). Its API follows net/http’s client:

resp, err := http.Get("https://example.com/api")
if err != nil { ... }
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)

Response bodies are streamed: read them incrementally (bufio, json.Decoder) to process large responses without holding them in memory. Waiting for the server notices query cancellation (the request then fails with the cancel error, and PostgreSQL raises it). A request can’t outlive the transaction: it’s closed when the transaction ends. Only http:// and https:// URLs are allowed, also when following redirects.

Importing this package links the extension with libcurl (it must be installed, with its headers, to build).

Index#

Variables#

var DefaultClient = &Client{}

DefaultClient is used by Get, Post and Do.

Functions#

func CanonicalHeaderKey#

func CanonicalHeaderKey(s string) string

CanonicalHeaderKey returns the canonical form of a header name: “content-type” -> “Content-Type”.

Types#

type Client#

type Client struct {
	// Timeout limits the whole request, reading the body included (0: no
	// limit). Connecting is limited to 30 seconds (or Timeout if shorter).
	Timeout time.Duration
	// Header is added to every request.
	Header Header
}

Client sends requests.

func (*Client) Do#

func (c *Client) Do(req *Request) (*Response, error)

Do sends a request and returns once the response headers are received.

func (*Client) Get#

func (c *Client) Get(url string) (*Response, error)

Get sends a GET request.

func (*Client) Post#

func (c *Client) Post(url, contentType string, body []byte) (*Response, error)

Post sends a POST request.

type Error#

type Error struct {
	Method, URL string
	Err         string
}

Error is a failed request (connection, TLS, timeout…).

func (*Error) Error#

func (e *Error) Error() string
type Header map[string][]string

Header is a set of HTTP headers. Keys are canonical (Content-Type).

func (Header) Add#

func (h Header) Add(key, value string)

Add adds a value to the header.

func (Header) Del#

func (h Header) Del(key string)

Del removes the header.

func (Header) Get#

func (h Header) Get(key string) string

Get returns the first value of the header, or “”.

func (Header) Set#

func (h Header) Set(key, value string)

Set replaces the values of the header.

type Request#

type Request struct {
	Method string // GET if empty
	URL    string
	Header Header
	Body   []byte // nil: no body
}

Request is an HTTP request.

func NewRequest#

func NewRequest(method, url string, body []byte) *Request

NewRequest returns a request.

type Response#

type Response struct {
	StatusCode int    // e.g. 200
	Status     string // e.g. "200 OK"
	Header     Header
	Body       io.ReadCloser
	Request    *Request
}

Response is an HTTP response. Its Body must be closed (or read to the end).

func Do#

func Do(req *Request) (*Response, error)

Do sends a request with DefaultClient.

func Get#

func Get(url string) (*Response, error)

Get sends a GET request with DefaultClient.

func Post#

func Post(url, contentType string, body []byte) (*Response, error)

Post sends a POST request with DefaultClient.