user3423407
user3423407

Reputation: 341

How to do a GET request with node.js passing a JWT token in header

I have been trying to do a GET request to my server which has is running locally on port 4000.

I generate a JWT token and pass it in the header as follows

var request = require('request');
var options = {
  'method': 'GET',
  'url': 'localhost:4000',
  'headers': {
    'JWT': '<JWT PASTED HERE>',
    'Content-Type': 'application/json'
  }
};
request(options, function (error, response) { 
  if (error) throw new Error(error);
  console.log(response.body);
});

But I keep getting


{"errors":[{"title":"invalid_request","id":"Requesting stuff","meta":{"server-time":1591980353},"errorCode":"bad-request","status":400,"detail":"This JWT has invalid path parameter"}],"error_description":"This JWT has invalid path parameter","error":"invalid_request"}

My JWT is correctly created, I verified it in https://jwt.io/

Is it because 'request' module is deprecated in node.js?

Is there another way I can achieve the below?

Upvotes: 0

Views: 2940

Answers (1)

Ashish Choubey
Ashish Choubey

Reputation: 457

Try this one

var request = require('request');
var options = {
  'method': 'GET',
  'url': 'localhost:4000',
  'headers': {
    'Authorization': 'Bearer <JWT PASTED HERE>',
    'Content-Type': 'application/json'
  }
};
request(options, function (error, response) { 
  if (error) throw new Error(error);
  console.log(response.body);
});

or

var options = {
  'method': 'GET',
  'url': 'localhost:4000',
  'headers': {
    'Authorization': 'JWT <JWT PASTED HERE>',
    'Content-Type': 'application/json'
  }
};

Bearer or JWT depends how it is defined in backend

Upvotes: 1

Related Questions