Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unsupported BodyInit type when using fetch #6025

Closed
peter0072000 opened this issue Feb 19, 2016 · 11 comments
Closed

unsupported BodyInit type when using fetch #6025

peter0072000 opened this issue Feb 19, 2016 · 11 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@peter0072000
Copy link

Hey,
When I using fetch to post a request on 0.20.0/0.19.0 version , it get 'unsupported BodyInit type' error:

following is request :

var object = {
    method: 'POST',
    body: {
        'loginId': 'helloreact',
        "password": 'd4da22ee9d210bab31d1c3ef8b3674a6'
    }
};

fetch(url, object)
.then((response) => response.text())
.then((responseData) => {})
.catch(function(err) {});
@ms88privat
Copy link

@peter0072000 please try body: JSON.stringify(obj)

@christopherdro
Copy link
Contributor

@peter0072000 And make sure to specify the header type. Will look something like this...

var object = {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body:JSON.stringify( {
       'loginId': 'helloreact',
       'password"' 'd4da22ee9d210bab31d1c3ef8b3674a6'
    })
};

fetch(url, object)
.then((response) => response.text())
.then((responseData) => {})
.catch(function(err) {});

@christopherdro
Copy link
Contributor

Related #2538

@xahon
Copy link

xahon commented Aug 10, 2017

Our server doesn't support plain/text media type in body. Tried to set 'Content-Type' header to 'application/json' also not working. How can i send request body in json?

@sfratini
Copy link

@xahon What do you mean? If your server does not support sending plain/text in the body it could mean it does not support sending POST methods which you need to send a body. If what you meant is that you cannot change the header I would just send all in one big string like so "{'text':'message'}".

@oleksandrriabykh
Copy link

@sfratini means you are sending body as plain text - "{'text':'message'}" but we need to send as object like {'text':'message'}

@oleksandrriabykh
Copy link

why do we need to convert json object to string in the post request? sorry, but it looks like sh*t and its really annoying when you have to do something like typeof options.body === 'string' ? options.body : JSON.stringify(options.body),

@dhcmega
Copy link

dhcmega commented Apr 11, 2018

Isn't the problem related to the answer and not the request?

@pavlea
Copy link

pavlea commented Apr 18, 2018

Totally agree with @oleksandrriabykh it became boring explaining backend guys that I'm limited to send them stringified body only. They were in disbelief when told them

@sfratini
Copy link

You can all install another fetch dependency and override the global.fetch. I am doing that right now in order to add a timeout to fetch and I am not having any of these issues.

@alexwasner
Copy link

alexwasner commented May 4, 2018

@sfratini I tried your solution with

import fetch from 'react-native-fetch-polyfill'
GLOBAL.fetch = fetch

and had no success. Is this the override you used?
To be more specific, it fixes this issue, but then throws Request is not a constructor in the polyfill

@facebook facebook locked as resolved and limited conversation to collaborators May 24, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

10 participants