John Wei
John Wei

Reputation: 23

React-native POST request in android over https return network error

Tried to create user account through https POST request under react-native with axios, while it always failed on android with a 'network error'.

axios('https://'+DEST_URI, {
 method: 'post',
 data: account,
 headers: {
   'Accept': 'application/json',
 }
 then(...)

The same https POST request works fine on iOS.

Changed to http, the same POST request works on android as well.

Also tried GET request through https on android, it could retrieve data from the backend server as expected.

Any idea about it?

Linked image is the output from console log, output from console log

Upvotes: 2

Views: 8331

Answers (3)

lucasfeliciano
lucasfeliciano

Reputation: 86

Related to the issue Upgrade to OkHttp3 on the react-native repository.

Deactivate http2 from your proxy that is what causing the problem. You can try to upgrade to react-native v0.27 they mention in the change log that they fixed this problem but for me didn't work.

I had to disable the http2 feature in ngnix to make it work.

disabling http2 really is this simple, just change this line in your ngnix config file from:

listen 443 ssl http2;

to

listen 443 ssl;

then reload your config:

service nginx reload

More info about http2 and ngnix can be found here

Upvotes: 1

Shahen Hovhannisyan
Shahen Hovhannisyan

Reputation: 645

I guess you have not a internet permission in your AndroidManifest.xml file.

Please add this line into you AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET" />

Upvotes: 0

Irfan Ayaz
Irfan Ayaz

Reputation: 797

I was facing the same issue with android 'POST' requests. Turned out to be headers issue as mentioned in the link: https://github.com/facebook/react-native/issues/5222#issuecomment-170239302

Adding the following headers fixed the issue for me

headers = {
'Accept': 'application/json',
'Authorization': 'Bearer ' + this.authToken,
'Content-Type': 'application/x-www-form-urlencoded',
}

Upvotes: 3

Related Questions