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
POSTandPUT. - For form data, set
Content-Typetoapplication/x-www-form-urlencodedand passkey=value&key2=value2as the body. - For response status codes and headers, or concurrent requests, use
http.
Functions
Section titled “Functions”curl::request
Section titled “curl::request”curl::request(url: string, method: string = "GET", headers: hashmap = {}, data: string = "", capture_output: boolean = true)Executes a curl command to perform an HTTP request.
| Parameter | Type | Default | Description |
|---|---|---|---|
url | string | The URL to which the request will be sent. | |
method | string | "GET" | The HTTP method (e.g., "GET", "POST") (default "GET"). |
headers | hashmap | {} | A hashmap of headers to include in the request (default {}). |
data | string | "" | Optional data to send with the request, used for POST and PUT (default ""). |
capture_output | boolean | true | If 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
Section titled “curl::get”curl::get(url: string, headers: hashmap = {}, capture_output: boolean = true)Sends a GET request to the specified URL.
| Parameter | Type | Default | Description |
|---|---|---|---|
url | string | The URL to request. | |
headers | hashmap | {} | Optional headers (default {}). |
capture_output | boolean | true | If true, returns the response body (default true). |
Returns string|integer — The response body or exit code.
curl::post
Section titled “curl::post”curl::post(url: string, headers: hashmap = {}, data: string = "", capture_output: boolean = true)Sends a POST request to the specified URL.
| Parameter | Type | Default | Description |
|---|---|---|---|
url | string | The URL to request. | |
headers | hashmap | {} | Optional headers (default {}). |
data | string | "" | The request body (default ""). |
capture_output | boolean | true | If true, returns the response body (default true). |
Returns string|integer — The response body or exit code.
curl::put
Section titled “curl::put”curl::put(url: string, headers: hashmap = {}, data: string = "", capture_output: boolean = true)Sends a PUT request to the specified URL.
| Parameter | Type | Default | Description |
|---|---|---|---|
url | string | The URL to request. | |
headers | hashmap | {} | Optional headers (default {}). |
data | string | "" | The request body (default ""). |
capture_output | boolean | true | If true, returns the response body (default true). |
Returns string|integer — The response body or exit code.
curl::delete
Section titled “curl::delete”curl::delete(url: string, headers: hashmap = {}, capture_output: boolean = true)Sends a DELETE request to the specified URL.
| Parameter | Type | Default | Description |
|---|---|---|---|
url | string | The URL to request. | |
headers | hashmap | {} | Optional headers (default {}). |
capture_output | boolean | true | If true, returns the response body (default true). |
Returns string|integer — The response body or exit code.