1

Here's my angular method

getGiftList(url: string){
let q = this.baseUrl + url;
let headers = new HttpHeaders();
let authToken = 'Bearer ' + this.authService.currentUser.token;

console.log(q); //Log the url ...
console.log(authToken); Log the token ...

headers.set('Authorization', authToken)

return this.http.get(q, {
  observe: 'response' 
  ,headers: headers
})

}

And this is the Asp.net core method

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class GiftController : Controller
{
    [HttpGet("api/giftsByGiver/{email}")]        
    public IActionResult getGiftBasicRecordsByGiver(string email, int cpg = 1)
    {
       //
    }
}

When I make a request from my angular code, I get a 401 error. However, when I copy/peste the same values that I logged to Postman and run, I'm getting the right result.

Am I missing something?

Thanks for helping

2
  • have you checked whether you have an authorization header in postman? They can be hard to notice, you have to open the headers tab Commented Dec 28, 2017 at 23:08
  • @PaulDegnan, for the sake of the development and given that I'm still learning, I've set up policies to be allow any origin, method, and header. Commented Dec 28, 2017 at 23:14

1 Answer 1

5

HttpHeaders is immutable. Calling set() doesn't mutate it. It returns a new instance of HttpHeaders. So you need

headers = headers.set('Authorization', authToken);

Or simpler, if you change the order of your instructions:

const authToken = 'Bearer ' + this.authService.currentUser.token;
const headers = new HttpHeaders().set('Authorization', authToken);

You should verify your assumptions: just open the network panel of your browser dev tools, and check if what you're sending is what you think you're sending.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.