Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make API calls IPV6 compatible in react-native using the .fetch function?

I have developed an app (for IOS) written in react native which is working good in IPV4 networks. However, recently Apple has sent a message saying that my app did not work when tested in an IPV6 network. They told me that I had to make it compatible with IPV6-only networks too.

The question is, how can I make my existing API calls compatible with an IPV6 network?

My API uses the https protocol and it is called by domain name, not a raw IP.

A sample fetch call that I use is:

fetch(query,{
      method: 'post',
      headers: Utils.fetchHeaders('post')
      })
      .then(Utils.checkStatus)
      .then(Utils.parseJSON)
      .then(json => this._handleResponse(json))
      .catch(error => {

         this.setState({
          votingError: 'Sorry, there was an error!',
          loading: false
       });

      });

and the API endpoint is in following format:

https://www.myapidomain.com

any help appreciated.

like image 400
meteorite Avatar asked Jun 28 '16 23:06

meteorite


People also ask

How do I fetch API data in react native?

Step to run the application: Open the terminal and type the following command. Output: Now open localhost:300 and in the console, the data is fetched. 3. Async-Await: This is the preferred way of fetching the data from an API.

How do you catch network request failed in react native?

It's just that simple! Start your app as usual but don't forget to give an IP address and a port, this will help you solve the Network Request Failed error. And on your mobile app, make sure to use the correct URL in your request. Make sure that CORS is enabled on your backend to avoid errors related to CORS.


1 Answers

The fetch API internally uses NSURLSession which already supports IPv6:

Most apps will not require any changes because IPv6 is already supported by NSURLSession and CFNetwork APIs.

Source: https://developer.apple.com/news/?id=05042016a

Since you're not using IPv4 addresses with the fetch API you should be good to go.

So I suspect there's something else (maybe some 3rd party code?) not cooperating with this. Did Apple provide you with more details about what was not working?

I suggest you follow this tutorial to test it on your side and find out what's incorrect.

like image 170
Jean Regisser Avatar answered Oct 13 '22 07:10

Jean Regisser