react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[Android] Not able to send a POST request

Open mbajur opened this issue 11 years ago • 14 comments

Hi, for some misterious reason, i'm not able to send POST requests using fetch() method. My request code looks like that:

fetch('http://localhost:3000/api/v1/oauth/token', {method: "POST", headers: {}, body:
  JSON.stringify({
    username: "username", 
    password: "pass", 
  })})
      .then((response) => response.json())
      .then((responseData) => {
          console.log(responseData.body)
      })
      .done();

However, running the code above does not actually fires a request to given endpoint (there is no any request log entry on the server that endpoint belongs to) but some other one which returns a following exception:

TypeError: Network request failed
    at self.fetch.xhr.onload (http://localhost:8081/index.android.bundle?platform=android:8335:8)
    at XMLHttpRequest._sendLoad (http://localhost:8081/index.android.bundle?platform=android:7959:1)
    at XMLHttpRequest.setReadyState (http://localhost:8081/index.android.bundle?platform=android:7949:6)
    at XMLHttpRequest.callback (http://localhost:8081/index.android.bundle?platform=android:7926:6)
    at MessageQueue.__invokeCallback (http://localhost:8081/index.android.bundle?platform=android:2635:10)
    at http://localhost:8081/index.android.bundle?platform=android:2547:8
    at guard (http://localhost:8081/index.android.bundle?platform=android:2494:1)
    at MessageQueue.invokeCallbackAndReturnFlushedQueue (http://localhost:8081/index.android.bundle?platform=android:2546:1)
    at messageHandlers.executeJSCall (http://localhost:8081/debuggerWorker.js:25:73)
    at DedicatedWorkerGlobalScope.onmessage (http://localhost:8081/debuggerWorker.js:42:5)

It looks like a typical network issue that occurs when there is no internet connection available on the device or i forget to run adb reverse tcp:8081 tcp:8081 but it's not that. I can tell that because just changing POST method to GET in the code above works like it should. Request comes to the given endpoint, log entry is present and everything acts like expected.

The same goes for PUTs and DELETEs, they all raises the exception above, just the GETs not.

Regards, Mike

ps. i'm on HEAD of react-native repo

mbajur avatar Nov 02 '15 14:11 mbajur

the same error on HTTP PUT/POST request with com.facebook.react:react-native:0.13.0

atom992 avatar Nov 02 '15 15:11 atom992

I see the same when I try to run the app on the local server. I do have react-native 0.13.0.

edo1493 avatar Nov 02 '15 21:11 edo1493

Does it work if you set the content-type header? See https://facebook.github.io/react-native/docs/network.html#fetch

alexkrolick avatar Nov 03 '15 00:11 alexkrolick

@whokilledtheelectricmonk Thank you very much,it works now.

atom992 avatar Nov 03 '15 03:11 atom992

Nope, still nothing.

On 3 November 2015 at 01:38, Alex Krolick [email protected] wrote:

Does it work if you set the content-type header? See https://facebook.github.io/react-native/docs/network.html#fetch

— Reply to this email directly or view it on GitHub https://github.com/facebook/react-native/issues/3838#issuecomment-153204457 .

mbajur avatar Nov 03 '15 07:11 mbajur

@whokilledtheelectricmonk it worked when I started developing on Android, now I still have it, but I have moved to 0.13.0 and it gets stuck there.

edo1493 avatar Nov 03 '15 08:11 edo1493

Oh, no! my bad, it actually works now :) Thanks @whokilledtheelectricmonk !

On 3 November 2015 at 09:04, Edoardo Moreni [email protected] wrote:

@whokilledtheelectricmonk https://github.com/whokilledtheelectricmonk it worked when I started developing on Android, now I still have it, but I have moved to 0.13.0 and it gets stuck there.

— Reply to this email directly or view it on GitHub https://github.com/facebook/react-native/issues/3838#issuecomment-153283546 .

mbajur avatar Nov 03 '15 08:11 mbajur

Solved, yes.

edo1493 avatar Nov 03 '15 08:11 edo1493

I'm still having this issue on Android, using 0.14. Content-type and accept headers are both set to the proper format.

madwed avatar Nov 09 '15 17:11 madwed

@madwed Can you provide us with some sample code?

christopherdro avatar Nov 10 '15 11:11 christopherdro

@christopherdro figured out my issue, in the android VM localhost points to the device, sending the request to my ip or 10.0.2.2 gets it out of the device

madwed avatar Nov 10 '15 15:11 madwed

@madwed Nice! @mbajur Can we go ahead and close this issue now?

christopherdro avatar Nov 10 '15 20:11 christopherdro

Sure thing! Thanks again :)

mbajur avatar Nov 10 '15 22:11 mbajur

Hi, pls give some example for JSON parsing(POST) in React Native Android

Surendharopus avatar Jul 04 '17 09:07 Surendharopus