Home > Back-end >  My GET request works fine in POSTMAN and Curl, but not in JavaScript code
My GET request works fine in POSTMAN and Curl, but not in JavaScript code

Time:12-04

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.

CodePudding user response:

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

CodePudding user response:

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.

  • Related