Module eco.http.client

HTTP/HTTPS/WebSocket client.

This module provides a simple HTTP/1.1 client with optional TLS support.

Supported URL schemes:

  • http, https
  • ws, wss (HTTP upgrade handshake only)

For https/wss, this module uses eco.ssl.connect internally and sets opts.server_name to the URL host for SNI.

Functions

new () Create a new HTTP client.
request (method, url[, body[, opts]]) Perform an HTTP request.
get (url[, opts]) Convenience wrapper for GET.
post (url[, body[, opts]]) Convenience wrapper for POST.
body_with_file (name) Use a file as request body.
form () Create a multipart form body.

Class client

client:close () Close the underlying connection.
client:sock () Get the underlying connected socket.
client:request (method, url[, body[, opts]]) Perform a request using this client.

Class body_form

body_form:add (name, value) Add a simple form field.
body_form:add_file (name, path) Add a file field.


Functions

new ()
Create a new HTTP client.

Returns:

    client
request (method, url[, body[, opts]])

Perform an HTTP request.

This is a convenience wrapper that creates a temporary client, performs the request, and closes the connection.

opts options commonly used:

  • timeout (number) request timeout in seconds (default 30).
  • headers (table) extra request headers.
  • body_to_file (string) write response body to the given file path.
  • ipv6 (boolean) resolve AAAA records.
  • mark (number) SO_MARK for sockets.
  • device (string) SO_BINDTODEVICE for sockets.
  • nameservers (table) DNS servers (see eco.dns.query).
  • TLS: ca, cert, key, insecure (passed to eco.ssl.connect).

Parameters:

  • method string HTTP method, e.g. "GET", "POST".
  • url string Request URL.
  • body string, body_file or body_form Request body. (optional)
  • opts table Options table. (optional)

Returns:

    table

    resp Response table:

    • code (number)
    • status (string)
    • headers (table)
    • body (string) (omitted when body_to_file is used)

Or

  1. nil On failure.
  2. string Error message.
get (url[, opts])
Convenience wrapper for GET.

Parameters:

Returns:

    table resp

Or

  1. nil
  2. string Error message.
post (url[, body[, opts]])
Convenience wrapper for POST.

Parameters:

Returns:

    table resp

Or

  1. nil
  2. string Error message.
body_with_file (name)
Use a file as request body.

The returned object can be used as the body argument of request.

Parameters:

Returns:

    body_file body

Or

  1. nil On failure.
  2. string Error message.
form ()
Create a multipart form body.

The returned object can be used as the body argument of request.

Returns:

    body_form

Class client

MQTT client object.

A client instance is created by calling mqtt.new.

client:close ()
Close the underlying connection.
client:sock ()
Get the underlying connected socket.

Returns:

    socket sock

Or

  1. nil When not connected.
  2. string Error message.
client:request (method, url[, body[, opts]])
Perform a request using this client.

For https/wss, TLS options in opts are passed to eco.ssl.connect.

Parameters:

Returns:

    table resp

Or

  1. nil On failure.
  2. string Error message.

Class body_form

Multipart form body returned by form.
body_form:add (name, value)
Add a simple form field.

Parameters:

Returns:

    boolean true
body_form:add_file (name, path)
Add a file field.

Parameters:

Returns:

    boolean true On success.

Or

  1. nil On failure.
  2. string Error message.
generated by LDoc 1.5.0 Last updated 2026-04-09 14:48:22