Fetch.Request (Fetch v0.1.0)

View Source

Encodes an HTTP/1.1 request into bytes.

POST /users HTTP/1.1\r\n
host: example.com\r\n
user-agent: fetch/0.1.0\r\n
content-type: application/json\r\n
content-length: 19\r\n
connection: close\r\n
\r\n
{"name":"Jon Snow"}

Header names are case-insensitive in HTTP, so the headers generated here are lowercase. User headers are sent as given.

Some headers describe how the message itself is framed on the connection. They are managed by the client and cannot be set by the user, because a wrong value would desynchronize client and server:

  • host — derived from the URL
  • content-length — computed from the body
  • transfer-encoding — not supported for requests
  • connection — close when the connection will not be reused; absent otherwise, because HTTP/1.1 connections are persistent by default

Every header name and value is validated. A value containing \r\n could otherwise inject extra headers or a whole second request.

Summary

Functions

Raises ArgumentError unless method is one the client supports.

Encodes a request. keep_alive: false adds connection: close, telling the server the connection ends after this response.

Types

headers()

@type headers() :: [{String.t(), String.t()}]

method()

@type method() :: :get | :head | :post | :put | :patch | :delete | :options

Functions

check_method!(method)

@spec check_method!(atom()) :: :ok

Raises ArgumentError unless method is one the client supports.

encode(method, url, headers, body, keep_alive)

@spec encode(method(), Fetch.URL.t(), headers(), iodata() | nil, boolean()) ::
  {:ok, iodata()} | {:error, {:request, term()}}

Encodes a request. keep_alive: false adds connection: close, telling the server the connection ends after this response.