0

I attempted to send a request with an authorization header using Angular's HttpClient but the header was not sent successfully.

Here's my code.

return this.http.get( API_URL + '/validate-email/' + email, this.getToken());

And here are my haders:

General:
Request URL: http://10.0.2.35:3000/validate-email/
Request Method: GET
Status Code: 401 Unauthorized
Remote Address: 10.0.2.35:3000
Referrer Policy: no-referrer-when-downgrade


Request Headers:
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Accept-Language: es-ES,es;q=0.9,gl;q=0.8,en;q=0.7,pt;q=0.6,la;q=0.5
Connection: keep-alive
Host: 10.0.2.35:3000
Origin: http://localhost:4200
Referer: http://localhost:4200/candidate-register
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36



Response Headers: 
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, 
Accept
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 49
Content-Type: application/json; charset=utf-8
Date: Mon, 21 May 2018 22:19:28 GMT
ETag: W/"31-iT0RTO+b5PSz1cX7BhK1mfQ8R6o"
X-Powered-By: Express

Does anyone know what's wrong?

sideshowbarker
  • 62,215
  • 21
  • 143
  • 153
Daniela Cortes
  • 485
  • 1
  • 4
  • 10
  • 1
    Hi @Daniela, could you manage to solve this issue. I'm facing same problem and not getting much help around it. – Sameer Awate Jul 11 '18 at 05:21
  • I could manage to figure it out myself. In my case, it was a custom implementation of [HttpInterceptor](https://angular.io/api/common/http/HttpInterceptor#httpinterceptor) done by another developer which was interfering my request headers. The code in there was trying to pick up Bearer authorization token from a wrong place. Just thought of noting it down here so that someone facing similar issue might want to find out if their code has any custom Interceptor implementation leading to issues they are facing. – Sameer Awate Jul 11 '18 at 12:18
  • Hi @SameerAwate, actually I had to use an interceptor to make it work and now I use it in every request and works fine now. – Daniela Cortes Jul 12 '18 at 17:34

2 Answers2

0

Try changing it to a POST request.

return this.http.post( API_URL + '/validate-email/' + email, this.getToken());

You are including the token in the body, but nothing is done with the body on a GET request.

More info: https://stackoverflow.com/a/983458/6544475

Max Jacobson
  • 271
  • 3
  • 10
0

You have not mentioned anywhere to set this.getToken() in the headers. Do the following
let requestHeaders = new HttpHeaders(); requestHeaders = requestHeaders.set('authorization', this.getToken());
and then use like
return this.http.get( API_URL + '/validate-email/' + email, {headers: requestHeaders});

See this for more details Angular HttpClient doesn't send header

Yousef khan
  • 1,456
  • 2
  • 11
  • 16