1

I started learning React Native a few weeks ago and I've been trying to create a mobile app of my website. The only problem is, on Android and only on Android, network requests that contain the Authorization header don't go through. I'm using Axios for network requests. This is an example of one of the requests. The function is called in componentDidMount.

axios.get('https://exampleurl.com', {headers: {
        'Authorization': 'Bearer ' + value}}) 
        .then(response => {
          console.log(response);
            Alert.alert("There is a response");
            this.setState({userData: response.data.data[0]});
         })
        .catch((error) => {
            Alert.alert("There is an error", error.message);
            that.setState({accessToken: ''});
        });

The response I get is that no access token is sent. Yes I'm sure that value is not null. The code works on IoS and in the browser. Funny thing is the call passes and works OK if the debugger is turned on. Is there any reason why only Android doesn't work?

Is there something on the server side that needs to be configured to receive Android requests? The server is hosted on Amazon S3. Is there something maybe on my side that needs to be configured? All other network requests work in Axios (the ones without an Authorization header).

Thank you in advance!

2 Answers 2

2

If this helps anyone in the future, after checking the server logs it turns out that, it did in fact send the Authorization header, except every header field was sent in lowercase. The server was configured to only accept Authorization and not authorization, so the server simply returned a response that it didn't get that header. This only happened on android. As to why android sends it in lowercase I still don't know.

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

Comments

0

Try setting your token in axios defaults. I'm not sure if it fits your case but worked for me when I had the same issue. Since you are using react-native and you have to store token on device (or retrieve it from your api for the first time), you can set it in axios defaults so that you won't have to set it on every axios request.

try this when you had your token:

axios.defaults.headers.common['Authorization'] = 'Bearer ' + value;

Comments

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.