REST
This node is used to connect to external REST endpoints to make requests and expose data. It supports request content in JSON format.
You initiate the request by sending a signal to the Fetch input. Any inputs and outputs to the node are specified in the request
and response
scripts.
Request and response script
The REST node uses a script much like the Script node to prepare the request and parse the response. Any variables with the prefix Inputs. and Outputs. will be exposed as inputs and outputs to the node. So you can use these inputs in the request script to prepare the REST call, and the outputs in the response script to parse the response into outputs.
Request
The first script is used to prepare the request before it is sent to the REST endpoint. The Inputs object contains the values of the inputs specified in the section above at the time the request was initiated. The Request object contains details on the request that can be adjusted if necessary.
- resource - Contains the resource for the request, i.e. the URL.
- method - Contains the method of the request.
- headers - This is a map containing all headers to the request, add or remove headers in the function.
- parameters - This is a map containing all parameters that will be appended to the URL of the request, add or remove in the function.
- content - Contains the content of the request, the content will be encoded as JSON when the request is made.
The example below will create two inputs that you can connect to this node from other data in your applications. It modifies the headers and content for the request.
Request.header["some-important-header"] = Inputs.ImportantHeader;
Request.content = {
"send-me": Inputs.SendMe,
};
When the request is completed and we have a response the corresponding response function will be invoked.
Response
In the response function, you can parse the response and set the appropriate outputs of the REST node using the Outputs map provided as an argument to the response function. In the function, you will also have access to an object called Response with the following content.
- status - The status code of the response.
- content - The content of the response.
- request - This contains the request object from the request function above that was used to make the request.
The REST node looks at the Content-Type
header to determine how to parse the response from the server. If the type is JSON it will use JSON.parse
and for XML it'll convert it to a JavaScript object.
Array Example
The following example assumes the response content is an array of objects. It is converted to a Noodl Array with Noodl Objects and sends it to the output Items.
Example Response Content:
[
{
"MyValue": 1
},
{
"MyValue": 2
}
]
Script:
Outputs.Items = Response.content.map((o) => Noodl.Object.create(o));
Items is now an array that can be sent to a Repeater or used in another node.
Object Example
The following example assumes the response content is an object. It converts the object to a Noodl Object and sends it to the output Items.
Example Response Content:
{
"title": "my title",
"description": "my cool description"
}
Script:
Outputs.Item = Noodl.Object.create(Response.content);
We can also call Response.content.description
immediately to extract certain properties.
Inputs
Signal | Description |
---|---|
Fetch | Connect a signal to this input to trigger a request. |
Cancel | Send a signal to this input to cancel an ongoing request. |
Data | Description |
---|---|
Resource | This is the resource endpoint that the REST request will use, i.e. the url. |
Method | This is the method that will be used for the request. Supported methods are GET, PUT, POST, PATCH and DELETE. |
Request | Here you can edit the request script. |
Response | Here you can edit the response script. |
Request and response inputs
Any variable of the Inputs object that you access in your scrips will automatically become an input of the node.
Outputs
Signal | Description |
---|---|
Success | A signal will be emitted on this output if the request receives a successful status code (2XX). |
Failure | A signal will be emitted on this output on all other status codes of if another type of failure occurs. |
Request and response outputs
Any variable of the Outputs object that you access in your scrips will automatically become an output of the node.