Skip to content

curl

The curl package makes HTTP requests by running the curl program, which must be installed and on your PATH. It is a simple, synchronous alternative to the http package, convenient for quick scripts.

body = curl::get("https://httpbin.org/get")
println body
headers = {"Content-Type": "application/json"}
println curl::post("https://httpbin.org/post", headers, '{"lang": "kiwi"}')
println curl::put("https://httpbin.org/put", headers, '{"status": "ok"}')
println curl::delete("https://httpbin.org/delete")

Each function returns the response body as a string. Pass false as the last argument (capture_output) to get curl’s exit code instead:

code = curl::get("https://example.com", {}, false)
println code == 0 ? "reachable" : "failed"

Notes:

  • Requests run with curl -s, so progress output is suppressed.
  • A request body is only sent for POST and PUT.
  • For form data, set Content-Type to application/x-www-form-urlencoded and pass key=value&key2=value2 as the body.
  • For response status codes and headers, or concurrent requests, use http.
curl::request(url: string, method: string = "GET", headers: hashmap = {}, data: string = "", capture_output: boolean = true)

Executes a curl command to perform an HTTP request.

ParameterTypeDefaultDescription
urlstringThe URL to which the request will be sent.
methodstring"GET"The HTTP method (e.g., "GET", "POST") (default "GET").
headershashmap{}A hashmap of headers to include in the request (default {}).
datastring""Optional data to send with the request, used for POST and PUT (default "").
capture_outputbooleantrueIf true, returns the response body as a string; otherwise returns the exit code (default true).

Returns string|integer — The response body if capture_output is true, or the exit code otherwise.

curl::get(url: string, headers: hashmap = {}, capture_output: boolean = true)

Sends a GET request to the specified URL.

ParameterTypeDefaultDescription
urlstringThe URL to request.
headershashmap{}Optional headers (default {}).
capture_outputbooleantrueIf true, returns the response body (default true).

Returns string|integer — The response body or exit code.

curl::post(url: string, headers: hashmap = {}, data: string = "", capture_output: boolean = true)

Sends a POST request to the specified URL.

ParameterTypeDefaultDescription
urlstringThe URL to request.
headershashmap{}Optional headers (default {}).
datastring""The request body (default "").
capture_outputbooleantrueIf true, returns the response body (default true).

Returns string|integer — The response body or exit code.

curl::put(url: string, headers: hashmap = {}, data: string = "", capture_output: boolean = true)

Sends a PUT request to the specified URL.

ParameterTypeDefaultDescription
urlstringThe URL to request.
headershashmap{}Optional headers (default {}).
datastring""The request body (default "").
capture_outputbooleantrueIf true, returns the response body (default true).

Returns string|integer — The response body or exit code.

curl::delete(url: string, headers: hashmap = {}, capture_output: boolean = true)

Sends a DELETE request to the specified URL.

ParameterTypeDefaultDescription
urlstringThe URL to request.
headershashmap{}Optional headers (default {}).
capture_outputbooleantrueIf true, returns the response body (default true).

Returns string|integer — The response body or exit code.