Search code examples
authorizationhttprequestnode-red

Using http-request node and bearer authentication to connect to REST API


For an application I need to connect to a REST API using Bearer authentication. The token is valid for 3 hours so I cannot fill it in statically in the http-request node itself.

I already tried to pass it as an object but every time the server returned a 401 code with the extra text that the token is not valid. When configuring the token inside of the http-request node, the server returns a 200 response with that same token.

I am testing it with an inject node which is connected to a function node which is connected to the http-request node. The function node contains the following code:

msg.headers = {};
msg.headers['Authorization'] = 'Bearer -mytoken-';

return msg;

Can somebody explain me why the token works when configured in the http-request node but not when passed as an object? Are there any settings that need to be done in the http-request node in order to accept the bearer token as an object?


Solution

  • Try this workaround posted in Node-RED forum:

    Workaround: Go to the http request node, select Use authentication, select basic authentication, leave username and password blank, select Done to save. On returning the 'use authentication' will be deselected. Authorization header should now be passed correctly when set in the incoming message.

    Credit: meesern