This is for creating a new token, but how and where do I add the comment to the token?
my fonction
error
This is for creating a new token, but how and where do I add the comment to the token?
my fonction
Hi and welcome.
I think you will need to explain more, do you have documentation of this API. Maybe an exaple flow with the data you are trying to insert into your http request.
P.s best to supply functions in text form as well as data, then people who wish to help you can easily provide example back.
Some help full info for future use.
There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path/value for any data item.
Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.
Also as you are new to node-red there are the essentials videos which will give you lots of helpful insights.
Hello,
I modified my code so that the token is saved and reused for the next function, but I still get the same error: "Token does not exist". So I don't see where my mistake is.
Here is my code:
To create and store the token:
// Parse msg.payload because it's a JSON string
let data = JSON.parse(msg.payload);
// Check if the code is 200 (success)
if (data.code === 200 && data.data && data.data.token) {
let token = data.data.token;
// Log part of the token
node.warn("Extracted token: " + token.substring(0, 1000000000));
// Store the token in flow memory
flow.set("authToken", token);
} else {
node.error("Error in response: " + msg.payload);
}
return msg;
To retrieve and use the token:
// Retrieve the token
let token = flow.get("authToken");
msg.headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + token,
"x-auth-token": token,
"x-access-token": token
};
// Data to send
msg.payload = {
id: 1,
storeId: "1886369875899715584",
icon: "ico...monster",
item: "Coucou",
item2: "hello!",
qrcode: "jsdqsdkfhsdjf...",
quantity: 40,
image: ""
};
return msg;
Also, when I use Postman, everything works perfectly, even when I use the token generated by this function.
So I really don't understand where the problem is.
Also, in Postman, my cURL request looks like this:
curl --location 'http://10.64.231.215:9194/esl/goods/updateToStore' \
--header 'Content-Type: application/json' \
--header 'token: eyJhbGciOiJSUzUxMiJ9.eyJzdWIiOiIxODg2MzY4Njg5NzAwMjEyNzM2IiwiZXhwIjoxNzQ3OTE1MDIyfQ.LLTNHZyoLDzB82uqodPrKOkaegwg0zmxEHY8t7jsbGeF5tBOk8-wmYphbAdd4q2vne4RsS6wHu8pB1JKg-XFi2YVubyS5HOVhXB3MDCMPzQ2_iwlNtXe9ym_Boqs0MlgcyH3po7k5oi6CBRHxZ3EoI8txqiZf5kdksHCEH3BPac' \
--data '{
"id": "1",
"storeId": "1886369875899715584",
"icon": "icofont-angry-monster",
"item": "Coucou",
"item2": "Hello!",
"qrcode": "jsfdsqkfhsdjf kjqsdhf kjqsdhkjfhsdk",
"quantity": "40",
"image": ""
}'
When I use this cURL in Postman, everything works perfectly, even with the token generated by my function.
However, I can't just copy this cURL command into the Node-RED function node, because the function node expects JavaScript code, not a raw cURL or JSON string. In Node-RED, it's recommended to build the request using JavaScript objects and let the HTTP request node handle the actual HTTP call.
Can you show the payload returned from the bearer authorization.
In my flow, I have two debug nodes at the top to retrieve and use the token.
I can see in Debug 4 that the token is missing from the headers. Is that normal?
I had a similar problem when i had to puh data through keycloak oauth. Since it is not my keycloak instance, the solution was a small wrapper script arround curl ...
i remember that the support says something that there were multiple requests where the header was send as the POST-data
You have to put the header information in your http request ...almost like your curl.. se below example I'm using
Debug 4 is the response not the request so the bearer token would not be in the header normally.
Is the http request post or get?