react-nativereact-native-ios

Fetch not sending Authorization header in ios


I use two fetch functions, one to log in another get data from my API.

login works fine but the second fetch throws an error in ios. it works fine on android

Authorization header is not being sent from ios

this is my code:

import { Alert } from 'react-native';
import { rootUrl } from './index';
import { translate } from '../../config/translation';
import store from './../store';
import NavigatorService from '../../config/navigator';

export default function fetchTree(Authorization) {
  fetch(`${rootUrl}/tree`, {
    method: 'GET',
    headers: {
      Authorization,
      'Content-Type': 'application/json',
    },
  })
    .then((response) => {
      console.log(response);
      store.dispatch({ type: 'loading', payload: false });
      if (response.status !== 200) {
        Alert.alert(
          translate('Access Denied'),
          translate('You are not Authorized to view this content'),
          [{ text: translate('OK') }],
          {
            cancelable: false,
          },
        );
        return 0;
      }
      return response.json();
    })
    .then((responseJson) => {
      if (responseJson !== 0) {
        const tree = responseJson[0].tree;
        NavigatorService.reset('Home', { tree });
      }
    });
}

this is the response I get from the server:

enter image description here

Solution:

The URL needs a trailing /

So it should have been ${rootUrl}/tree/ instead of ${rootUrl}/tree


Solution

  • The URL needs a trailing /

    So, it should have been ${rootUrl}/tree/ instead of ${rootUrl}/tree