currently I am working on a simple service written in nodejs for RocketChat. But the module 'request' doesnt send my header parameters for authenticaton. Here are the details:

//Login
request({
    url: rocketChatURL + '/api/v1/login', 
    method: 'POST',
    body: JSON.stringify({
        'user': rocketChatUser, 
        'password': rocketChatPass
        })
    }, (err, res, body) => {
 if (err) { return console.log(err); }
 console.log(body);
 obj = JSON.parse(body);
 console.log(obj);
 rocketChatAuthToken = JSON.stringify(obj.data.authToken);
});

//Channel Infos
var options = {
    url: rocketChatURL + '/api/v1/channels.info?roomName=' + rocketChatRoom, 
    method: 'GET',
    //json: true,
    headers: {
        "X-User-Id": rocketChatUserID,
        "X-Auth-Token": rocketChatAuthToken,
        }
};

Login returns the following object:

{ status: 'success',
  data:
   { userId: 'XYZ',
     authToken: 'JS_VNjOnFpicTIdhD4n1WOdvl950wOEa-LDDACqg_yN',
     me:
      { _id: 'XYZ',
        name: 'myTechuser',
        emails: [Array],
        status: 'online',
        statusConnection: 'online',
        username: 'myTechuser',
        utcOffset: 2,
        active: true,
        roles: [Array],
        settings: [Object],
        email: '[email protected]' 
        } 
    } 
}

First of all theres the login. Afterwards I want to get some channel information. Therefore the UserID and AuthToken have to be provided via header (see https://rocket.chat/docs/developer-guides/rest-api/channels/info/). The response is always a HTTP 401 with the error message "You must be logged in to do this". After some research with 'request-debug' I saw that the request module just sends the "X-User-Id" Header but not the "X-Auth-Token" (see snippet below). This is the reason why the authentication fails. Because if I send the request manually via CURL (and of course with header parameters) the authentication works well.

Could you explain to my why the request-module doesnt send the second header option with the request?

{ request:
   { debugId: 3,
     uri:
      'https://chat.xxxxx.com/api/v1/channels.info?roomName=myRoom',
     method: 'GET',
     headers:
      { 'X-User-Id': 'XYZ',
        host: 'chat.xxxxx.com' } } }


{ response:
   { debugId: 3,
     headers:
      { date: 'Mon, 29 Apr 2019 19:02:03 GMT',
        'content-type': 'application/json',
        'transfer-encoding': 'chunked',
        connection: 'close',
        server: 'nginx/1.13.12',
        'x-instance-id': 'g47SaWLkqdM2ZrhQ8',
        'access-control-allow-origin': '*',
        'cache-control': 'no-store',
        pragma: 'no-cache',
        vary: 'Accept-Encoding',
        'set-cookie': [Array],
        'strict-transport-security': 'max-age=31536000; includeSubDomains' },
     statusCode: 401,
     body:
      '{"status":"error","message":"You must be logged in to do this."}' } }

0 Answers