Fetch.Request (Fetch v0.1.0)
View SourceEncodes 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 URLcontent-length— computed from the bodytransfer-encoding— not supported for requestsconnection—closewhen 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
Functions
@spec check_method!(atom()) :: :ok
Raises ArgumentError unless method is one the client supports.
@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.