node-red-axios-wrapper 1.0.1
axios wrapper node
Node-RED Axios Wrapper
axios wrapper node
Overview
This provides a generic wrapper function for Axios, enabling you to make HTTP requests within Node-RED with configurations similar to those used in Postman. The function follows the subscribe pattern and is designed to be easily integrated into your Node-RED workflows.
Parameters
config
(Object): The request configuration object that should include properties likeurl
,method
,headers
,params
, anddata
.
Returns
A Promise that resolves with the response data as a JSON object.
Usage in Node-RED Subflow
The function is executed using the subscribe pattern, which means it will process incoming messages (msg.payload
) containing request configurations:
return axiosWrapper(msg.payload)
.then(responseData => {
msg.payload = responseData;
return msg;
})
.catch(error => {
msg.payload = { status: 'error', message: error };
return msg;
});
Functionality
Header Configuration:
- Merges any headers provided in the
config
with a default'Content-Type'
of'application/json'
.
- Merges any headers provided in the
Request Configuration:
- Constructs the request configuration using properties from the input config (
url
,method
,headers
,params
, anddata
).
- Constructs the request configuration using properties from the input config (
Debug Logging (if enabled):
- Logs the request configuration to the Node-RED console if debugging is enabled globally.
HTTP Request Execution:
- Uses Axios to make the HTTP request.
- Resolves with response data on success.
- Rejects with an error message if the request fails.
Example Input Message (msg.payload
)
{
"url": "https://api.example.com/data",
"method": "POST",
"headers": {
"Authorization": "Bearer <token>"
},
"data": {
"key1": "value1"
}
}
Example Output Message (msg.payload
)
- Success: Contains the response data from the API.
- Error: Contains a status of 'error' and the error message.
Installation
- Clone this repository or download the subflow file directly.
- Import the subflow into your Node-RED instance via the "Import" feature in the Node-RED UI.
Dependencies
- Axios: Ensure you have Axios installed in your Node-RED environment to use this wrapper function.
npm install axios
Configuration
- The
config
object should contain at least aurl
and amethod
. Other properties likeheaders
,params
, anddata
are optional but commonly used.
Debugging
Set the global debug setting in Node-RED to enable logging of request configurations:
global.set('debug', true);
This will print out the request configuration being sent via Axios to help with debugging issues.
This subflow function is designed to simplify making HTTP requests within Node-RED by leveraging Axios while maintaining flexibility similar to Postman configurations.