Reputation: 1772
I am using HTTPClient Module of Angular 4 for retrieving data from a database. The corresponding service has the following method,
getPosts() {
const httpHeaders = new HttpHeaders().set('Content-Type', 'application/json');
// tslint:disable-next-line:max-line-length
httpHeaders.set('Authorization for JWT', 'Token');
return this.http.get('http://localhost:9090/api/user' + '?user_id=' + this.user.id.toString(),
{headers: httpHeaders,
responseType: 'json'});
}
I am calling this method from a component as follows,
this.Servie.getPosts().subscribe(pos => {
console.log(pos);
});
But I am getting an error as follows in server side,
java.lang.RuntimeException: JWT Token is missing
error at the client side,
Failed to load http://localhost:9090/api/user?user_id=1: Response for preflight has invalid HTTP status code 500.
Please correct me where I am going wrong?
I Have made changes according to discussion below, but still problem is there as follows,
I think I have messed it up right now, I made the following changes as follows,
This is the code written in service,
const httpHeaders = new HttpHeaders()
.set('Content-Type', 'application/json')
// tslint:disable-next-line:max-line-length
.set('RequestAuthorization', 'Bearer ' + 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI5ODg0NjUxMjMzIiwidXNlcklkIjoiNSJ9.gwiZcx5I8rInXJGANvS9twupXjdjrzFdZNZ0K85u-KA8LXXDgDf27mUzUoiEyxRMg');
return this.http.get('http://localhost:9090/user' + '?user_id=' + this.user.id.toString(),
{headers: httpHeaders});
And i am calling the above service as follows,
this.userServie.getPosts().map((res: Response) =>
console.log(res.text()));
But the service is not hitting, i am not seeing the GET method in network tab of browser development tools. Where i am wrong ? Please correct me.
Upvotes: 2
Views: 8365
Reputation: 34465
Once you figure out the real headers to send I think you'll have 2 problems:
Issue #1
HttpHeaders
class is immutable
So you need to have something like this
const httpHeaders = new HttpHeaders()
.set('Content-Type', 'application/json')
.set('Authorization', 'Bearer [yourTokenHere]')
instead of declaring httpHeaders
and calling set without chaining the call
const httpHeaders = new HttpHeaders();
//The line below has no effect on the httpHeaders instance
httpHeaders.set('Authorization', 'Bearer [yourTokenHere]');
Issue #2
Also, did you make sure that CORS was configured properly on your java server?
You are getting an error about missing token for the preflight
request (i.e. the one with the Options method). AFAIK, browsers do not send custom headers like Authorization
for preflight requests. So, the server should not check the token for the OPTIONS request (otherwise, you'll always end up getting 401 as tokens won't be sent)
Upvotes: 0
Reputation:
Headers have defined values.
httpHeaders.set('Authorization for JWT', 'Token');
Should be
httpHeaders.set('Authorization', 'Bearer ' + token);
If you provide random tokens to your server, of course it will tell you that the token is missing. In your case, you're sending this
Authorization for JWT --> Token
Upvotes: 1