Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving 401 status with Safari not Chrome in React

The problem we are facing is the following: When using Safari as a browser, rather than Chrome, we receive a 401 status on a get api call. The technologies we are using are React and Django Rest Framework. In React we are also using axios. Again, all is fine when we use Chrome as our browser (no 401 error is given and authentication seems to be fine), but when we switch to Safari, it does not work. We should also note that when testing with Postman, if we store a token in our authentication header no 401 status is given (it works).

To try and solve this, we have tried different types of authentication classes in our Django backend, and ensured CORS was in our settings. We also ensured that we followed the react component lifecycle for proper mounting, and included the appropriate info necessary for each api call. We believe this to be a client side issue.

Please see the image below for a brief description of network requests / responses we see, and our api call.

Network Responses and api call

like image 240
Makai Avatar asked May 24 '18 18:05

Makai


People also ask

How do I fix 401 unauthorized error in react?

In response to a client request, the server may return a 401 Unauthorized error. You must correctly catch it, for example, clear the state and redirect to the authorization page. To solve this problem, we wrote a custom Middleware which, in our opinion, is the best solution.

How do I fix request failed with HTTP status 401 unauthorized?

You need to set the credentials in you application when you initialise the webService object. This will set the credentials of the request to the current user executing the application.

Why am I getting a 401 error?

What is a 401 Unauthorized Error? A 401 Unauthorized Error is an HTTP status code that indicates that the server received an unverified request. In human terms, this means that the website you're trying to access won't load until you log-in with a valid user ID and password.

What does the HTTP status code 401 indicates?

The HyperText Transfer Protocol (HTTP) 401 Unauthorized response status code indicates that the client request has not been completed because it lacks valid authentication credentials for the requested resource.


1 Answers

So it turns out the root of our problem was that we did not end one of our api urls with a forward slash, resulting in a 301 then a 304. Chrome was able to autocorrect without an issue, but Safari, IE, and firefox cannot.

like image 110
Makai Avatar answered Sep 24 '22 15:09

Makai