std::curl
A library for making HTTP requests using the system's curl
binary.
You can find a set of examples here.
Functions
-
DELETE(url: str, body: str|buf = "") -> Response|err
-
Convenience function for making a simple
DELETE
request. -
GET(url: str) -> Response|err
-
Convenience function for making a simple
GET
request. -
HEAD(url: str) -> Response|err
-
Convenience function for making a simple
HEAD
request. -
OPTIONS(url: str) -> Response|err
-
Convenience function for making a simple
OPTIONS
request. -
PATCH(url: str, body: str|buf) -> Response|err
-
Convenience function for making a simple
PATCH
request. -
POST(url: str, body: str|buf) -> Response|err
-
Convenience function for making a simple
POST
request. -
PUT(url: str, body: str|buf) -> Response|err
-
Convenience function for making a simple
PUT
request.
Requests
-
Request() -> Request
-
Creates a new
Request
object.
Request
objects have the following public methods:
-
:append_args(*args: str) -> Request
-
Appends custom arguments for the
curl
binary.Returns the request object to enable chaining.
-
:body(value: str|buf) -> Request
-
Sets the request's body to
value
.Returns the request object to enable chaining.
-
:debug() -> Request
-
Turns on debug mode. Debug output is printed to the standard error stream.
Returns the request object to enable chaining.
-
:header(value: str) -> Request
-
Adds a header to the request.
Returns the request object to enable chaining.
-
:http_auth(username: str, password: str) -> Request
-
Adds authentication credentials in Basic HTTP Authentication format.
Returns the request object to enable chaining.
-
:url(value: str) -> Request
-
Sets the request's URL.
Returns the request object to enable chaining.
-
:param(key: any, value: any) -> Request
-
Adds a key/value parameter pair to the request —
key
andvalue
will be automatically stringified if they are not already strings.If the request is one of (
GET
,HEAD
,OPTIONS
), the parameter pair will be URL-encoded and appended to the URL's query string.If the request is one of (
POST
,PUT
,PATCH
,DELETE
), the parameter pair will be URL-encoded and appended to the request's body inapplication/x-www-form-urlencoded
format. The appropriateContent-Type
header will be automatically added to the request.Returns the request object to enable chaining.
-
:timeout(seconds: f64) -> Request
-
Sets a timeout for the request, measured in (fractional) seconds. If the request hangs for longer than the specified timeout, it will return an
err
.Returns the request object to enable chaining.
-
:DELETE() -> Response|err
-
Sends a
DELETE
request. -
:GET() -> Response|err
-
Sends a
GET
request. -
:HEAD() -> Response|err
-
Sends a
HEAD
request. -
:OPTIONS() -> Response|err
-
Sends an
OPTIONS
request. -
:PATCH() -> Response|err
-
Sends a
PATCH
request. -
:POST() -> Response|err
-
Sends a
POST
request. -
:PUT() -> Response|err
-
Sends a
PUT
request.
Responses
Response
objects have the following public fields:
class Response { # The response's HTTP status code. pub var code: i64; # The response's body. pub var body: buf; # The response's headers, including the status line. pub var raw_headers: str; # The response's headers as a map of key/value pairs. # Keys are converted to lower-case ASCII. pub var headers: map[str, str]; }
Response
objects have the following public methods:
-
:from_json() -> map|vec|str|i64|f64|bool|null|err
-
Attempts to parse the response body as JSON. Returns the resulting value.
Returns an
err
if the response body is not valid JSON. -
:has_header(key: str) -> bool
-
Returns
true
if the response has a header namedkey
, wherekey
is a case-insensitive string. -
:header(key: str) -> str|err
-
Returns the value of the header
key
, wherekey
is a case-insensitive string.Returns an
err
if the header does not exist.