1

When I make my get request with POSTMAN or Curl, it works just fine and I get the expected response. I just set type to GET, insert my URL and insert a key named token/email and set it to a token/email I get from another service. But when I try to call the same service in code like this:

   function getAll() {
        const myHeaders = new Headers({
            'Accept': 'application/json',
            'token': 'MY TOKEN',
            'email': 'MY EMAIL'
        });


        return fetch('https://apitul', {
            headers: myHeaders,
            method: 'GET'
        })
        
        .then(response => {
            if (response.status === 200) {
            return response.json();
            } else {
            throw new Error('Something went wrong on api server!');
            }
        })
        .then(response => {
            console.debug(response);
        }).catch(error => {
            console.error(error);
        });
    }

I get 401 error. I also found out that the server is not receiving the token in the request header. Any ideas how I can fix this? Cheers.

6
  • 1
    Error code 401 means Unauthorized. Are you sure you provided the correct authentication? Commented Dec 3, 2021 at 23:38
  • Hello @code, I sent the same parameters via curl or postman and the return is 200 (my curl: $ curl -H "token: MYTOKEN" -H "email: MYEMAIL" APIuRL Commented Dec 3, 2021 at 23:46
  • Have you confirmed that the headers are indeed sent with the correct values from JavaScript? Commented Dec 4, 2021 at 0:04
  • Yes, I even generated the code by postman and it gave the same error ..this is code for javascript fetch: var myHeaders = new Headers(); myHeaders.append("token", "mytoken"); myHeaders.append("email", "myemail"); myHeaders.append("Cookie", "CVid=9injvuffqn6gj3824uh87st595"); var requestOptions = { method: 'GET', headers: myHeaders, redirect: 'follow' }; fetch("api_url", requestOptions) .then(response => response.text()) Commented Dec 4, 2021 at 0:06
  • @r31sr4r Well, if you look at the curl command you provided, you'll see token: MYTOKEN. You need to supply a token in your request in your JavaScript or else you'll get an unauthorized error. Commented Dec 4, 2021 at 0:45

2 Answers 2

3

To send your token in header use this: 'Authorization': 'Bearer yourToken'

Sign up to request clarification or add additional context in comments.

1 Comment

In this API a had to pass the token on header with a key 'token' ....
1

Try to generate code using Postman, lets see if it helps. See attached.

Try to generate code using Postman, let's see if it helps. See attached.

4 Comments

I generate code e retrive this: curl --location --request GET 'api_url' \ --header 'token: mytoken' \ --header 'email: mymail \ --header 'Cookie: CVid=9injvuffqn6gj3824uh87st595' .... in curl i recive the json response ... but in javascript i had Status401 Unauthorized VersionHTTP/2 Transferred437 B (0 B size) Referrer Policyno-referrer-when-downgrade
this is code for javascript fetch: var myHeaders = new Headers(); myHeaders.append("token", "mytoken"); myHeaders.append("email", "myemail"); myHeaders.append("Cookie", "CVid=9injvuffqn6gj3824uh87st595"); var requestOptions = { method: 'GET', headers: myHeaders, redirect: 'follow' }; fetch("api_url", requestOptions) .then(response => response.text()) .then(result => console.log(result))
Check if the token is still valid. @r31sr4r
apparently the token is still valid. I made the call directly through firefox and edited the header passing the parameters and it worked, however through javascript it still doesn't work. I verified that on the console the error is now: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at api_url. (Reason: CORS preflight response did not succeed)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.