[Android] Not able to send a POST request
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
the same error on HTTP PUT/POST request with com.facebook.react:react-native:0.13.0
I see the same when I try to run the app on the local server. I do have react-native 0.13.0.
Does it work if you set the content-type header? See https://facebook.github.io/react-native/docs/network.html#fetch
@whokilledtheelectricmonk Thank you very much,it works now.
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 .
@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.
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 .
Solved, yes.
I'm still having this issue on Android, using 0.14. Content-type and accept headers are both set to the proper format.
@madwed Can you provide us with some sample code?
@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 Nice! @mbajur Can we go ahead and close this issue now?
Sure thing! Thanks again :)
Hi, pls give some example for JSON parsing(POST) in React Native Android