Http Client
Perform an HTTP request to a specified endpoint.
When to use?
Whenever you have some data that you need from a separate web end point, or if perhaps you're trying to update something. This will allow you to perform HTTP requests.
How to use:
-
Select a Scope type from the drop-down
- Scope type installation is for the duration of the whole installation and has the longest life span. This is essentially stored on the device.
- Scope type session is for the duration of the current session. This will persist until the user exits the skill.
- Scope type request is for the duration of a single request. This is the shortest life span and is useful if you're only interested in a bit of data short term.
-
From the Parameters drop down, pick where you want to store responses.
- Storing parameters in the Service level makes them available anywhere else in the service.
- Storing them at Block level makes them availalbe only in the current block.
- In the Result name field enter the name the result will be stored under.
- In the Endpoint URL field enter the URL you wish to send the request to.
- From the Content type drop-down, select the content type you're expecting to get in the headers. If you're unsure, leave it at
Auto
.
-
In the HTTP method drop-down choose between
GET
, POST
, or PUT
methods
- You can cache
GET
requests by entering a Cache timeout value, numbered in seconds.
- For
POST
and PUT
requests, you can add a JSON Body to send.
- In the Timeout field enter an amount, in seconds, the request can hang for before it is terminated.
- In the Headers field you can add HTTP headers, and in the Parameters field you can add URL query parameters (
?parameter=value
)
-
The element contains two flows to be executed, depending on the request success: OK and NOK
- To access the body of the response, use the
body
property on your result variable. For example, if for Result name you chose row
, then you could access the body with ${row.body}
- To access the HTTP status of the request, use the
status
attribute on your response, e.g. ${row.status}
.
- For errors, you can access the message with the
error
parameter, e.g. ${row.error}
.
Example of usage
Here's an example of how you might fetch and read the horoscope for any given sign. Start off by using a Set Parameter
element to set a parameter called sign
to any of the horoscope signs, in lower case. For example, virgo
Next, place an HTTP Query
element. Configure it as follows:
- Scope type: Installation
- Parameters: Block Params
- Result name:
row
-
Endpoint URL:
http://ohmanda.com/api/horoscope/${sign}/
- Note: The URL is extrapolated! We're using the sign you previously set as part of the URL here.
- Content type:
JSON
or Auto
- HTTP method:
GET
- Timeout: 10
-
For output, place a
Text Response
element in the OK flow. In the Text field of the Text Response
element, write the following (without the quotation marks)
The current horoscope for ${sign}
is: ${row.body.horoscope}