I have a .NET 6 Web API application that uses JWT authentication. The front end is an angular application. When I make an API call with the bearer token in the header, it returns the 401 error code. But with the same bearer token, I am able to call the same API from Swagger and get the expected data back.
I am using HttpClient
to make the API calls:
import { HttpClient, HttpHeaders } from '@angular/common/http'; ... constructor(private httpClient: HttpClient private router: Router) { } ... const tokenHeader = new HttpHeaders({ "Authorization": `Bearer ${token}` }); const options = { headers: tokenHeader }; return this.httpClient.post<any>(url, postParams, options);
request:
Preflight request: