Request

class Request

An HTTP Request

method: str

the HTTP method for this request

url: table

The parsed url of this request

http_version: str

The http version from the request first line

headers: Headers

The HTTP headers for this request

body: str

The contents of the request’s body

_source: fun():str

ltn12 source

_parsed_headers: boolean
_headers: Headers
_received_body: boolean
static source(source)

Construct a request from a ltn12 source function

Parameters

source (fun():str) – this should always return a single line when called

Return type

Request

get_headers()

Get the headers for this request

parsing the incoming stream of headers if not already parsed

Return type

Headers or str or nil

_fill_headers()

read from the socket filling in the headers property

get_body()

Get the contents of this request’s body

if not yet received, this will read the body from the socket

Return type

str or nil or str or nil

content_length()

Get the value from the Content-Length header that should be present

for all http requests

Return type

number or nil or str or nil

static new(method, url)

Construct a request Builder

Parameters
  • method (str) – an http method string

  • url (str or table) – the path for this request as a string or as a net_url table

Return type

Request

add_header(key, value)

Add a header to the internal map of headers

note: this is additive, so adding X-Forwarded-For twice will cause there to be multiple X-Forwarded-For entries in the serialized headers

Parameters
  • key (str) – The Header’s key

  • value (str) – The Header’s value

Return type

Request

set_content_type(ct)

Set the Content-Type header for this request

convenience wrapper around self:add_header(‘content_type’, len)

Parameters

ct (str) – The mime type to add as the Content-Type header’s value

Return type

Request

set_content_length(len)

Set the Content-Length header for this request

convenience wrapper around self:add_header(‘content_length’, len)

Parameters

len (number) – The Expected length of the body

Return type

Request

append_body(chunk)

append the provided chunk to this Request’s body

Parameters

chunk (str) – The text to add to this request’s body

Return type

Request

serialize()

Serialize this request into a single string

Return type

str

as_source()

Serialize this request as an ltn12 source that will

provide the next line (including new line characters). This will split the body on any internal new lines as well

Return type

fun():str