0

I am having 401 Unauthorized error when sending get request. I am confused because I can send request over the terminal with curl or postman but not from the code (localhost)

CODE

   const carId = '2F3A228F6F66AEA580'
    var query = 'https://api.mercedes-benz.com/experimental/connectedvehicle/v1/vehicles/'.concat(carId).concat('/doors')

    console.log(query)
    console.log(this.state.access_token)

    var bearer = 'Bearer ' + this.state.access_token

    console.log(bearer)
    const response = await fetch(query, {
      method: 'GET',
      headers: {
        'accept': 'application/json',
        'authorization': bearer
      },
    })

Note : I am using the same access_token

Community
  • 1
  • 1
Berkin
  • 1,565
  • 5
  • 22
  • 48

1 Answers1

2

I am having 401 Unauthorized error when sending get request.

No, you aren't. Read the error message more carefully.

It says that you are making an OPTIONS request, not a GET request.

The browser is making a preflight OPTIONS request because you are trying to make a cross-origin request with an authorization header.

The server appears to be testing for authorization for the OPTIONS request. This fails because the browser has not been given permission to send a cross-origin Ajax request with credentials.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335