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,httpsws,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:
- 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.
optsoptions 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
code(number)status(string)headers(table)body(string) (omitted whenbody_to_fileis used)
resp Response table:
Or
- nil On failure.
- string Error message.
- get (url[, opts])
-
Convenience wrapper for
GET.Parameters:
Returns:
-
table
resp
Or
- nil
- string Error message.
- post (url[, body[, opts]])
-
Convenience wrapper for
POST.Parameters:
Returns:
-
table
resp
Or
- nil
- string Error message.
- body_with_file (name)
-
Use a file as request body.
The returned object can be used as the
bodyargument of request.Parameters:
- name string File path.
Returns:
-
body_file
body
Or
- nil On failure.
- string Error message.
- form ()
-
Create a multipart form body.
The returned object can be used as the
bodyargument 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
- nil When not connected.
- string Error message.
- client:request (method, url[, body[, opts]])
-
Perform a request using this client.
For
https/wss, TLS options inoptsare passed to eco.ssl.connect.Parameters:
- method string HTTP method.
- url string Request URL.
- body string, body_file or body_form Request body. (optional)
- opts table See request. (optional)
Returns:
-
table
resp
Or
- nil On failure.
- 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
- nil On failure.
- string Error message.