Skip to content

Network and HTTP

HTTP

query(array form_values) : string

Converts an associative array into a form-style string, used for e.g. application/x-www-form-urlencoded requests or HTTP query strings.

query(['country': 'Curaçao', 'population': 158665]) 
// country=Cura%C3%A7ao&population=158665

request(string url, string body, string method = 'GET', array headers, bool override = false, timeout = 5) : array

Sends a HTTP request and returns an array with the following keys containing response data:

  • content (contains a cURL error message in case of an error)
  • status (null in case of an error)
  • headers
  • url

The headers should be an array of strings, for example:

[
  'Content-Type: application/json',
  'Accept: application/json, text/plain, */*'
]

To get a JSON document, validate if valid JSON, and get a property:

response = request('https://example.com')

decoded = json_decode(response['content'])
if (decoded) {
  value = decoded['value']
}

If override is set to true, none of the content from the original request is included (e.g. query strings, headers, content.)

multipart(string url, array items, string method = 'POST', array headers, num timeout) : array

Sends a HTTP Multipart request, e.g. for uploading files.

name (the form name value) and content are required in the items array; the rest is optional.

timeout specifies the request timeout in seconds.

The return value is an array with the following keys containing response data:

  • content (contains a cURL error message in case of an error)
  • status (null in case of an error)
  • headers
  • url

Example:

multipart(
  'https://example.com/file-upload', [
    [
      'name': 'file[]',
      'filename': 'file1.txt',
      'content': 'hello world',
      'headers': ['Header1': 'value', 'Header2': 'othervalue']
    ],
    [
      'name': 'client_id',
      'content': 'abcd123',
    ]
  ],
  'POST',
  ['Api-Key: xxxx']
)

url_decode(string value) : string

Returns an URL-decoded version of value.

url_encode(string value) : string

Returns an URL-encoded version of value.

url_encode('here\'s a value') // here%27s+a+value