http_request

Table of contents

Makes a request to the passed target and fetches the body. Useful for API connections. HTTP Headers and params can be set for the request. Inside the block the response is accessible as response variable.

Arguments

Accepts the following arguments:

target
The target for the HTTP request. E.g. https://www.apiserver.com/api/v1/api_endpoint

params
Multiple key/value attributes that serve as url query parameters for GET requests.

If the request method is POST, PUT, PATCH or DELETE, these params will be set in the body. If a string is provided, this will be directly set to the body. If a Liquid object is provided, this will be mapped to JSON or www form, depending on the argument request_body_type

request_body_type

Used for POST, PUT, PATCH or DELETE request to determine the request body should be formatted, in case a Liquid object is given for params. Possible values: ”json”, ”www_form”. Defaults to ”json”

headers
Multiple key/value attributes that serve as request headers. The keys must be downcased and with underscores, but get converted to camelcased with dashes. E.g. {x_my_header_key: "header_val"}becomes the header: X-My-Header-Key: header_val.

method
Sets the request method: GET, POST, PUT or DELETE. Defaults to GET.

Available variables

The following variables are available inside the block:

response
The response of the request. If the response was JSON, the data converts to a Liquid object. You can use the to_json filter to turn it back to json.

params
The passed params as a Liquid object.

method
The request method. GET, POST, PUT or DELETE

target
The full target url, so including passed params. E.g. https://www.apiserver.com/api/v1/api_endpoint?param_key_1=param_val_1

Example

{% http_request target: 'https://www.apiserver.com/api/v1/api_endpoint', method: 'get', params: { param_key_1: 'param_val_1' }, headers: { authentication: 'Token token=ksuhf3ygjyw3fskuddh3uhr4hwr556h6j6eda' } %}

{{ response }}
{{ response | to_json }}

{% for response_item in response %}
  {{ response_item.result_key }}
{% endfor %}

{{ params }}

{% endhttp_request %}
Input
[{"result_key" => "result value 1"}, {"result_key" => "result value 2"}]
[{"result_key": "result value 1"}, {"result_key": "result value 2"}]

result value 1
result value 2

{'param_key_1' => 'param_val_1'}
Output

Note that the arguments params and headers have nested key/value pair arguments. These need to be surrounded by curly braces ({ ... }).

Advanced mode

If you want to retrieve more information about the response, you can provide an extra argument advanced: true. This will cause the response variable to be a liquid object where:

response.body is the body of the response.

response.code is the status code of the response (e.g. 200, 404 etc).

Example

Assuming the server returns “The response body from the server” as response body:


{% http_request target: 'https://www.apiserver.com/api/v1/api_endpoint', method: 'get', advanced: true %}
{{ response.body }}
{{ response.code }}
{% endhttp_request %}
Input
"The response body from the server"
200
Output

Need help?

Do you have any question which is not answered in this knowledge base? Contact us. We are here to help you.