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:

do pet = HTTP("https://example.com/pets/1").get().send()

// or, if no verb is specified, .get() is implicitly used
do pet2 = HTTP("https://example.com/pets/2").send()

say "This pet is called: {{pet.name}}"

You can also create more complex requests:

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

  • .disable_ssl_verify() : for situations where you know that the endpoint comes with an invalid SSL certificate, you can disable SSL verification to bypass this protection. This option should only be used if you know what you are doing!

Analyzing the response

You can check whether the call returned a response by using the .is_error() method on the response object.

The .get_info() method lets you inspect the response of HTTP calls, even when the call returns an error (in which case .get_info() will also contain the body):

.is_error() and .get_info() are very useful methods to correctly handle HTTP responses!

Last updated

Was this helpful?