How to pass JsonWebToken(JWT) through AngularJS











up vote
2
down vote

favorite
1












I created a Django RESTful API with JWT as authentication method, but unable to pass the token as headers using angularJS.



I think there is no error on my API, since I used the token I acquired by this script and tested it in Postman:
enter image description here



my JWT token authentication script is here:



 // Uses http.get() to load data from a single API endpoint
list() {
return this.http.get('api/', this.getHttpOptions());
}

// helper function to build the HTTP headers
getHttpOptions() {
return {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'JWT ' + this._userService.token
})
};
}


I tried using http.get() here. Thanks in advance!



the error will be like:



401 (Unauthorized)









share|improve this question
























  • are you seeing the header in the network request to api/ ?
    – karthick
    Nov 20 at 23:42










  • I am having the same issue using Django rest framework Token and Ionic3. The headers appear to be set in the request, Access-Control-Request-Headers authorization,content-type. When i manually resend the request in firefox with the header as, Authorization: Token xxxmyaccesstokenxxx it succeeds which leads me to believe that Ionic/Angular is not setting the token in a way Django can access it.
    – J.ward
    9 hours ago















up vote
2
down vote

favorite
1












I created a Django RESTful API with JWT as authentication method, but unable to pass the token as headers using angularJS.



I think there is no error on my API, since I used the token I acquired by this script and tested it in Postman:
enter image description here



my JWT token authentication script is here:



 // Uses http.get() to load data from a single API endpoint
list() {
return this.http.get('api/', this.getHttpOptions());
}

// helper function to build the HTTP headers
getHttpOptions() {
return {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'JWT ' + this._userService.token
})
};
}


I tried using http.get() here. Thanks in advance!



the error will be like:



401 (Unauthorized)









share|improve this question
























  • are you seeing the header in the network request to api/ ?
    – karthick
    Nov 20 at 23:42










  • I am having the same issue using Django rest framework Token and Ionic3. The headers appear to be set in the request, Access-Control-Request-Headers authorization,content-type. When i manually resend the request in firefox with the header as, Authorization: Token xxxmyaccesstokenxxx it succeeds which leads me to believe that Ionic/Angular is not setting the token in a way Django can access it.
    – J.ward
    9 hours ago













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





I created a Django RESTful API with JWT as authentication method, but unable to pass the token as headers using angularJS.



I think there is no error on my API, since I used the token I acquired by this script and tested it in Postman:
enter image description here



my JWT token authentication script is here:



 // Uses http.get() to load data from a single API endpoint
list() {
return this.http.get('api/', this.getHttpOptions());
}

// helper function to build the HTTP headers
getHttpOptions() {
return {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'JWT ' + this._userService.token
})
};
}


I tried using http.get() here. Thanks in advance!



the error will be like:



401 (Unauthorized)









share|improve this question















I created a Django RESTful API with JWT as authentication method, but unable to pass the token as headers using angularJS.



I think there is no error on my API, since I used the token I acquired by this script and tested it in Postman:
enter image description here



my JWT token authentication script is here:



 // Uses http.get() to load data from a single API endpoint
list() {
return this.http.get('api/', this.getHttpOptions());
}

// helper function to build the HTTP headers
getHttpOptions() {
return {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'JWT ' + this._userService.token
})
};
}


I tried using http.get() here. Thanks in advance!



the error will be like:



401 (Unauthorized)






angularjs django-rest-framework jwt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago

























asked Nov 20 at 23:32









hsbzzhz

317




317












  • are you seeing the header in the network request to api/ ?
    – karthick
    Nov 20 at 23:42










  • I am having the same issue using Django rest framework Token and Ionic3. The headers appear to be set in the request, Access-Control-Request-Headers authorization,content-type. When i manually resend the request in firefox with the header as, Authorization: Token xxxmyaccesstokenxxx it succeeds which leads me to believe that Ionic/Angular is not setting the token in a way Django can access it.
    – J.ward
    9 hours ago


















  • are you seeing the header in the network request to api/ ?
    – karthick
    Nov 20 at 23:42










  • I am having the same issue using Django rest framework Token and Ionic3. The headers appear to be set in the request, Access-Control-Request-Headers authorization,content-type. When i manually resend the request in firefox with the header as, Authorization: Token xxxmyaccesstokenxxx it succeeds which leads me to believe that Ionic/Angular is not setting the token in a way Django can access it.
    – J.ward
    9 hours ago
















are you seeing the header in the network request to api/ ?
– karthick
Nov 20 at 23:42




are you seeing the header in the network request to api/ ?
– karthick
Nov 20 at 23:42












I am having the same issue using Django rest framework Token and Ionic3. The headers appear to be set in the request, Access-Control-Request-Headers authorization,content-type. When i manually resend the request in firefox with the header as, Authorization: Token xxxmyaccesstokenxxx it succeeds which leads me to believe that Ionic/Angular is not setting the token in a way Django can access it.
– J.ward
9 hours ago




I am having the same issue using Django rest framework Token and Ionic3. The headers appear to be set in the request, Access-Control-Request-Headers authorization,content-type. When i manually resend the request in firefox with the header as, Authorization: Token xxxmyaccesstokenxxx it succeeds which leads me to believe that Ionic/Angular is not setting the token in a way Django can access it.
– J.ward
9 hours ago












2 Answers
2






active

oldest

votes

















up vote
1
down vote













Try this:



list() {
return this.http.get('api/', { this.getHttpOptions() });
}

getHttpOptions() {
const headers = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'JWT ' + this._userService.token
});
return headers;
}





share|improve this answer























  • Thank you, I tried in this way: return this.http.get(api/', {headers : this.getHttpOptions() });, but still doesn't work, same error code 401. I doubt the page hasn't refresh after I log in successful
    – hsbzzhz
    Nov 21 at 3:58










  • Sorry that didn't work. That's almost exactly the code I use and it work fine for me. Strange
    – brando
    Nov 21 at 4:54


















up vote
0
down vote













Same issue on JWT for CakePHP3, and follow header slove it, may it is helpful.



this.http.get('api/', { headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'JWT ' + this._userService.token,
'Authenticationtoken': 'JWT ' + this._userService.token
}) });





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403192%2fhow-to-pass-jsonwebtokenjwt-through-angularjs%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Try this:



    list() {
    return this.http.get('api/', { this.getHttpOptions() });
    }

    getHttpOptions() {
    const headers = new HttpHeaders({
    'Content-Type': 'application/json',
    'Authorization': 'JWT ' + this._userService.token
    });
    return headers;
    }





    share|improve this answer























    • Thank you, I tried in this way: return this.http.get(api/', {headers : this.getHttpOptions() });, but still doesn't work, same error code 401. I doubt the page hasn't refresh after I log in successful
      – hsbzzhz
      Nov 21 at 3:58










    • Sorry that didn't work. That's almost exactly the code I use and it work fine for me. Strange
      – brando
      Nov 21 at 4:54















    up vote
    1
    down vote













    Try this:



    list() {
    return this.http.get('api/', { this.getHttpOptions() });
    }

    getHttpOptions() {
    const headers = new HttpHeaders({
    'Content-Type': 'application/json',
    'Authorization': 'JWT ' + this._userService.token
    });
    return headers;
    }





    share|improve this answer























    • Thank you, I tried in this way: return this.http.get(api/', {headers : this.getHttpOptions() });, but still doesn't work, same error code 401. I doubt the page hasn't refresh after I log in successful
      – hsbzzhz
      Nov 21 at 3:58










    • Sorry that didn't work. That's almost exactly the code I use and it work fine for me. Strange
      – brando
      Nov 21 at 4:54













    up vote
    1
    down vote










    up vote
    1
    down vote









    Try this:



    list() {
    return this.http.get('api/', { this.getHttpOptions() });
    }

    getHttpOptions() {
    const headers = new HttpHeaders({
    'Content-Type': 'application/json',
    'Authorization': 'JWT ' + this._userService.token
    });
    return headers;
    }





    share|improve this answer














    Try this:



    list() {
    return this.http.get('api/', { this.getHttpOptions() });
    }

    getHttpOptions() {
    const headers = new HttpHeaders({
    'Content-Type': 'application/json',
    'Authorization': 'JWT ' + this._userService.token
    });
    return headers;
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 at 0:44

























    answered Nov 21 at 0:23









    brando

    5,43352851




    5,43352851












    • Thank you, I tried in this way: return this.http.get(api/', {headers : this.getHttpOptions() });, but still doesn't work, same error code 401. I doubt the page hasn't refresh after I log in successful
      – hsbzzhz
      Nov 21 at 3:58










    • Sorry that didn't work. That's almost exactly the code I use and it work fine for me. Strange
      – brando
      Nov 21 at 4:54


















    • Thank you, I tried in this way: return this.http.get(api/', {headers : this.getHttpOptions() });, but still doesn't work, same error code 401. I doubt the page hasn't refresh after I log in successful
      – hsbzzhz
      Nov 21 at 3:58










    • Sorry that didn't work. That's almost exactly the code I use and it work fine for me. Strange
      – brando
      Nov 21 at 4:54
















    Thank you, I tried in this way: return this.http.get(api/', {headers : this.getHttpOptions() });, but still doesn't work, same error code 401. I doubt the page hasn't refresh after I log in successful
    – hsbzzhz
    Nov 21 at 3:58




    Thank you, I tried in this way: return this.http.get(api/', {headers : this.getHttpOptions() });, but still doesn't work, same error code 401. I doubt the page hasn't refresh after I log in successful
    – hsbzzhz
    Nov 21 at 3:58












    Sorry that didn't work. That's almost exactly the code I use and it work fine for me. Strange
    – brando
    Nov 21 at 4:54




    Sorry that didn't work. That's almost exactly the code I use and it work fine for me. Strange
    – brando
    Nov 21 at 4:54












    up vote
    0
    down vote













    Same issue on JWT for CakePHP3, and follow header slove it, may it is helpful.



    this.http.get('api/', { headers: new HttpHeaders({
    'Content-Type': 'application/json',
    'Authorization': 'JWT ' + this._userService.token,
    'Authenticationtoken': 'JWT ' + this._userService.token
    }) });





    share|improve this answer

























      up vote
      0
      down vote













      Same issue on JWT for CakePHP3, and follow header slove it, may it is helpful.



      this.http.get('api/', { headers: new HttpHeaders({
      'Content-Type': 'application/json',
      'Authorization': 'JWT ' + this._userService.token,
      'Authenticationtoken': 'JWT ' + this._userService.token
      }) });





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Same issue on JWT for CakePHP3, and follow header slove it, may it is helpful.



        this.http.get('api/', { headers: new HttpHeaders({
        'Content-Type': 'application/json',
        'Authorization': 'JWT ' + this._userService.token,
        'Authenticationtoken': 'JWT ' + this._userService.token
        }) });





        share|improve this answer












        Same issue on JWT for CakePHP3, and follow header slove it, may it is helpful.



        this.http.get('api/', { headers: new HttpHeaders({
        'Content-Type': 'application/json',
        'Authorization': 'JWT ' + this._userService.token,
        'Authenticationtoken': 'JWT ' + this._userService.token
        }) });






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 at 4:40









        incNick

        1794




        1794






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403192%2fhow-to-pass-jsonwebtokenjwt-through-angularjs%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...