Skip to main content
EnZhoCR
Participating Frequently
Feb 19, 2023 11:58 AM UTC
Answered

JWT Authorization header

Hello Community,

i need help with this problem.

 

I would like to use the : 

JWT Authorization header using the Bearer scheme. Example: "Authorization: Bearer {token}"

 

Someone can help me? I know, how can read an xml from a APi but without a header autentication. I need to know how can i pass trow the header this kind of token.

 

Thanks a lot

    This topic is closed to new replies. Start a new post to keep the conversation going.
    JoãoCésar17023019
    JoãoCésar17023019Correct answer
    Community Expert
    Feb 20, 2023 12:13 PM UTC

    Hi.

     

    If you're using XMLHttpRequest, you could so something like this:

     

    var xhttp = new XMLHttpRequest();
    
    xhttp.onreadystatechange = function()
    {
        if (this.readyState == 4 && this.status == 200)
    		console.log("request complete");
    };
    
    xhttp.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem("jwtoken"));
    xhttp.open("GET", "filename", true);
    xhttp.send();

     

     

    https://www.w3schools.com/xml/xml_http.asp

    https://stackoverflow.com/questions/52685215/how-to-send-jwt-in-xmlhttprequest

     

    Have you tried it?

     

    Please let us know.

     

    Regards,

    JC

    6 replies

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    Feb 20, 2023 12:13 PM UTC

    Hi.

     

    If you're using XMLHttpRequest, you could so something like this:

     

    var xhttp = new XMLHttpRequest();
    
    xhttp.onreadystatechange = function()
    {
        if (this.readyState == 4 && this.status == 200)
    		console.log("request complete");
    };
    
    xhttp.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem("jwtoken"));
    xhttp.open("GET", "filename", true);
    xhttp.send();

     

     

    https://www.w3schools.com/xml/xml_http.asp

    https://stackoverflow.com/questions/52685215/how-to-send-jwt-in-xmlhttprequest

     

    Have you tried it?

     

    Please let us know.

     

    Regards,

    JC

    Translate
    EnZhoCR
    EnZhoCRAuthor
    Participating Frequently
    Feb 20, 2023 1:08 PM UTC

    Hello Joao, you already help me (i changed to a new account but in the past already asked and you answered) anyway thanks!

     i am trying to use something like this :

     

    var reqCurrent = new XMLHttpRequest();
    reqCurrent.open("GET", "URL", true);
    
    reqCurrent.setRequestHeader('Authorization', 'Bearer ' + 'Token');
    reqCurrent.addEventListener("load", currentLoader);
    reqCurrent.send();

     

    but i getting "CORS Policy error" , this is from server configuration or my fault?

    Thanks a lot, 

    Translate
    JoãoCésar17023019
    Community Expert
    Community Expert
    Feb 20, 2023 1:16 PM UTC

    Awesome! Really good to know!

     

    Have you tried something like this:

    xhttp.setRequestHeader("Access-Control-Allow-Origin", "*");

     

    If that doesn't work, then you probably have to configure the server.

    Translate