问题
im trying to learn how to add a http get request for the Actility plattform in Node red. For now i only recive an error 401 that the authorization bearer is not included.
Setup is done like this:
I get two codes from the plattform
curl -X GET --header 'Accept: application/json' --header 'Authorization: Bearer xxx' 'https://dx-api.thingpark.com/core/latest/api/devices?deviceEUI=xx&healthState=ACTIVE&statistics=true&extendedInfo=true'
First is the token bearer.
second is the request url.
https://dx-api.thingpark.com/core/latest/api/devices?deviceEUI=xxx&healthState=ACTIVE&statistics=true&extendedInfo=true
How can i create a flow that produces the answer correctly?
Thank you.
function setup
回答1:
The Javascript inside a function
node is sandboxed (runs in a virtual machine), and so you cannot use some features like "require". However, this is not a problem -- you can add any header information directly to the msg.headers
object, either in your function
node or even in a change
node.
You don't show us what data you are injecting, but according to the http request
node info, you can pass all of these (optional) fields as input that can become part of the request to the Actility system:
msg.url (string) If not configured in the node, this optional property sets the url of the request. msg.method (string) If not configured in the node, this optional property sets the HTTP method of the request. Must be one of GET, PUT, POST, PATCH or DELETE. msg.headers (object) Sets the HTTP headers of the request. msg.cookies (object) If set, can be used to send cookies with the request. msg.payload Sent as the body of the request.
Assuming you are injecting the payload data that you want to POST to Actility, you can just add the Auth headers you need using a simple function node that does something like this:
msg.method = "POST";
msg.headers = {
"Authorization": "Bearer xxx",
"Content-Type": "application/json"
};
return msg;
Or, let's say you were passing the bearer credential string into the function as the payload, and you have a fixed payload to send to Actility -- then your function could look something like this:
msg.method = "POST";
msg.headers = {
"Authorization": "Bearer " + msg.payload,
"Content-Type": "application/json"
};
msg.payload = { "foo": "bar" };
return msg;
Note: in order for these injected fields to be used, the http request
node cannot have previously defined their values as part of the node's configuration.
来源:https://stackoverflow.com/questions/49108022/node-red-lorawan-actility-plattform