Request Object

Request objects are received by splash:on_request callbacks; they are also available as response.request.

Attributes

Request objects has several attributes with information about a HTTP request. These fields are for information only; changing them doesn’t change the request to be sent.

request.url

Requested URL.

request.method

HTTP method name in upper case, e.g. “GET”.

request.headers

A Lua table with request HTTP headers (header name => header value). Keys are header names (strings), values are header values (strings).

Lookups are case-insensitive, so request.headers['content-type'] is the same as request.headers['Content-Type'].

request.info

A table with request data in HAR request format.

Methods

To change or drop the request before sending use one of the request methods. Note that these methods are only available before the request is sent (they has no effect if a request is already sent). Currently it means you can only use them in splash:on_request callbacks.

request:abort

Drop the request.

Signature: request:abort()

Returns: nil.

Async: no.

request:enable_response_body

Enable tracking of response content (i.e. response.body attribute).

Signature: request:enable_response_body()

Returns: nil.

Async: no.

This function allows to enable response content tracking per-request when splash.response_body_enabled is set to false. Call it in a splash:on_request callback.

request:set_url

Change request URL to a specified value.

Signature: request:set_url(url)

Parameters:

  • url - new request URL

Returns: nil.

Async: no.

request:set_proxy

Set a proxy server to use for this request.

Signature: request:set_proxy{host, port, username=nil, password=nil, type='HTTP'}

Parameters:

  • host
  • port
  • username
  • password
  • type - proxy type; allowed proxy types are ‘HTTP’ and ‘SOCKS5’.

Returns: nil.

Async: no.

Omit username and password arguments if a proxy doesn’t need auth.

When type is set to ‘HTTP’ HTTPS proxying should also work; it is implemented using CONNECT command.

request:set_timeout

Set a timeout for this request.

Signature: request:set_timeout(timeout)

Parameters:

  • timeout - timeout value, in seconds.

Returns: nil.

Async: no.

If response is not fully received after the timeout, request is aborted. See also: splash.resource_timeout.

request:set_header

Set an HTTP header for this request.

Signature: request:set_header(name, value)

Parameters:

  • name - header name;
  • value - header value.

Returns: nil.

Async: no.

See also: splash:set_custom_headers