HTTP Client
CSML includes a native HTTP client. The following verbs are accepted:
    GET
    POST
    PUT
    PATCH
    DELETE
HTTP will automatically add the following headers to any request:
    Content-Type: application/json;
    Accept: application/json
In other words, HTTP requests only allow you to send and query json-formatted data.

Performing a HTTP request

To build your request, you can chain explicit methods to the HTTP function. No request is actually performed until .send() is added, allowing you to gradually build your request.
Here is a simple example:
1
do pet = HTTP("https://example.com/pets/1").get().send()
2
3
// or, if no verb is specified, .get() is implicitly used
4
do pet2 = HTTP("https://example.com/pets/2").send()
5
6
say "This pet is called: {{pet.name}}"
Copied!
You can also create more complex requests:
1
do req = HTTP("https://example.com/pets").set({"authorization":"Bearer XXXXX"})
2
3
if (query_mode == "retrieve") {
4
// retrieve all the available pets from the API
5
do req = req.get()
6
}
7
else {
8
// this will create a new pet
9
do req = req.post({"name": "Oreo", "breed": "poodle"})
10
}
11
12
do res = req.send()
13
14
say "{{res}}"
Copied!
The available methods are:
    .get() / .post(body) / .put(body) / .patch(body) / .delete() : set the request verb, and add an optional JSON-formatted body
    .set(data): set the request headers, where {"x-api-key":"somevalue"} is added as x-api-key:somevalue headers
    .auth(username, password) set basic auth headers
    .query(data): set the query strings, where {"key":"value"} is automatically translated to ?key=value
Last modified 2mo ago
Export as PDF
Copy link