# Combining 2 http requests into one flow

**URL:** <https://discourse.nodered.org/t/combining-2-http-requests-into-one-flow/60107>\
**Category:** General\
**Created:** [20 March 2022 14:09 UTC](https://discourse.nodered.org/t/combining-2-http-requests-into-one-flow/60107 "2022-03-20T14:09:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![milner236](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@milner236](https://discourse.nodered.org/u/milner236)\
**Post date:** [20 March 2022 14:09 UTC](https://discourse.nodered.org/t/combining-2-http-requests-into-one-flow/60107/1 "2022-03-20T14:09:18Z")

</div>

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-

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/ea8984c766655c71a8d347d687c84147e4516cd0.png)

The first function node contains-

```auto
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-

```auto
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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/356c773f9e39b83ee445a64b2f354b012f48bc23.png)

I receive a json parse error. Any tips?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [20 March 2022 14:15 UTC](https://discourse.nodered.org/t/combining-2-http-requests-into-one-flow/60107/2 "2022-03-20T14:15:39Z")

</div>

> [@milner236](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![milner236](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@milner236](https://discourse.nodered.org/u/milner236)\
**Post date:** [20 March 2022 14:20 UTC](https://discourse.nodered.org/t/combining-2-http-requests-into-one-flow/60107/3 "2022-03-20T14:20:07Z")

</div>

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..

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 March 2022 14:22 UTC](https://discourse.nodered.org/t/combining-2-http-requests-into-one-flow/60107/4 "2022-03-20T14:22:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [20 March 2022 14:43 UTC](https://discourse.nodered.org/t/combining-2-http-requests-into-one-flow/60107/5 "2022-03-20T14:43:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![milner236](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@milner236](https://discourse.nodered.org/u/milner236)\
**Post date:** [20 March 2022 14:51 UTC](https://discourse.nodered.org/t/combining-2-http-requests-into-one-flow/60107/6 "2022-03-20T14:51:20Z")

</div>

The full debug message from the first flow that generates the API Token-  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/8776ec9a4f5df9b3c91164b15a088ad0b9b9c557.png)

The full debug message from the second flow that I use the token with bearer to authenticate-  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/7/a7cae3415b28191ec8fb61f63ba900a401554b76.png)

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

Thanks as usual guys.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [3 April 2022 14:52 UTC](https://discourse.nodered.org/t/combining-2-http-requests-into-one-flow/60107/7 "2022-04-03T14:52:09Z")

</div>

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