Token verification with headers

I need to send my jwt Token with the headers and it works fine in Postman but when i try it in Ionic with that Code.

ync setAnzahlTische() {
    var headers = new Headers();
    headers.append('Accept', 'application/json');
    headers.append('Content-Type', 'application/json' );
    this.storage.get('token').then((val) => {
      headers.set('Authorization', val);
    });
    headers.append('Access-Control-Allow-Origin', '*');
    headers.append('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    headers.append('x-Trigger', 'CORS');

    let tos = JSON.stringify(postData);

    this.http.post('http://127.0.0.1:8000/updatedata', tos, {responseType: 'text'})
        .subscribe(data => {

            this.router.navigate(['/home']);

        }, error => {
            console.log(error.message);
        });
    }

I have shortened the Code a little bit but the important things are still there. I get Access denied when i try to do the http.post but the JWT token is valid. I think the headers are undefined but im not sure.

So are a bunch of non-important things that should go away for clarity, such as all the header munging aside from Authorization, which isn’t actually being done.

When you write

this.storage.get('token').then((val) => {
  throwIntoOcean(val);
});

…I find it helpful to literally think of throwing things into the ocean, like the cliche of a message in a bottle. The stuff inside the then clause will get executed, but the only place you’ll be able to do things with certainty once it has is in that then clause.

So, to recap, your code gives a bunch of headers to the request that it doesn’t need (all of them you’re actually setting in time), is manually stringifying postData (which it shouldn’t do because it ruins automatic content type setting), and doesn’t set the one header you do need to set.

I would suggest this post as an example that you could follow instead.

Nice post You mentioned here, really worth reading. The problem with @Rotschaedl’s example here is that request get sent before getting data from storage?