Combining 2 http requests into one flow

Hello everyone.
There is an API process that involves 2 flows:

  • HTTP request with basic authentication (username and pw) that generates an API token.
  • Taking that API token and using it in another HTTP request with bearer authentication in order to get the wanted data.

Separately each flow works great.

I'd like to combine both flows in one.
This is the joined flow-

The first function node contains-

msg.headers = {}
msg.method = "POST"
msg.url = "https://example.com" ;
msg.headers["content-type"] = "application/json"


msg.payload = {
    
"username":"exampleusername",
"password":"examplepw",

};

return msg;

The first http request outputs the API token. I then use the change node to set msg.token to msg.payload.

This is the second function node-

msg.headers = {}
msg.method = "GET"
msg.url = "https://example.com" ;
msg.headers["content-type"] = "application/json"
msg.headers["secret"] = "examplesecret"
msg.headers["Authorization"] = "Bearer "+ msg.token


return msg;

This is the second http request node-
image

I receive a json parse error. Any tips?

The only thing to do is to examine what the response actually is. Change the node to not try to JSON parse the response, and then pass the output to a Debug node. You'll then be able to see what the API is returning, which could be an error message for example.

I receive an empty string.
However if I seperate the flow into 2 seperate flows, they work great. I receive the API token, which I then manually insert into the second flows http request under bearer authentication.

I believe the issue has something to do with setting the token into msg.token, but im not experienced (yet) enough to actually see whats wrong..

Feed the good and failing messages that you feed into the second request into debug nodes, set to Output Complete message. There must be an important difference between the two inputs.

You could also add a node.warn(headers) after you set the headers in second function, to see what the headers you send look like and if token is set correctly.

The full debug message from the first flow that generates the API Token-
image

The full debug message from the second flow that I use the token with bearer to authenticate-
image

Edit:
Just realized what was wrong. should have used msg.payload.jwt instead of msg.payload.

Thanks as usual guys.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.