Ravi Ravi - 1 year ago 59
Node.js Question

JWT authentication working with $http.get but not with $http.post

I am new to MEAN stack.
I am using jwt to authenticate api endpoint '/api/candidates'

In client-side/angular js service I have following entry

resumeFactory.get = function(candidateName) {
return $http.get('/api/candidates', {
headers: {
Authorization: 'Bearer '+ authenticationService.getToken()

on the server-side I have :

app.get('/api/candidates', auth, function (req, res) {
if (!req.payload._id) {
//user not logged in
res.status(401).json({"message": "no user logged in from candidates"});
else {
Candidate.find({}, function (err, candidates) {
if (err) {

This works fine. i.e. 'auth' is able to extract the token from the header

When i change everything to post instead of get :

on client-side


on server side :


I get an error from jwt as following:

UnauthorizedError: No authorization token was found

Any suggestions on what is happening?. I can work with get but i want to know why post does not work

Answer Source

With $http.post config is the third argument, not second (like in get) so update your parameters accordingly:

$http.post('/url', { cool: postData }, {
  headers: {
    Authorization: 'Bearer '+ authenticationService.getToken()

Better yet, use an interceptor to add the Authorization header to all requests.