1

I'm trying to get data from an authorized API in a Reactjs app and I'm still getting that 401(Unauthorized) error, also 'OPTIONS' request keeps popping up instead of GET request.

const api_call = await axios.get(`api-link`, {'Access-Control-Allow-Headers': "Origin, X-Requested-With, Content-Type, Accept", 'Access-Control-Allow-Origin' : "*", headers: {'Authorization': "bearer API-auth"}});

const response= await api_call.json();
console.log(response);
develop05
  • 487
  • 1
  • 9
  • 23
  • Can you say what API is? You only need `axios.get('link', {headers: {'Authorization: Bearer token'}}`. Have you tried with CAPS B? – lcssanches Aug 28 '18 at 00:25
  • You might find https://stackoverflow.com/questions/52047548/response-for-preflight-does-not-have-http-ok-status-in-angular helpful; basically the server shouldn’t require authentication for a CORS preflight request (the OPTIONS sent automatically as part of this request). – msbit Aug 28 '18 at 00:33
  • CORS is not something you handle from the frontend. It should be handled through the server you are making a call to, namely, the API. – Sujil Maharjan Aug 28 '18 at 02:38

0 Answers0