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' error is uncatchable #2538

Closed
despairblue opened this issue Sep 3, 2015 · 4 comments
Closed

'unsupported BodyInit type' error is uncatchable #2538

despairblue opened this issue Sep 3, 2015 · 4 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@despairblue
Copy link
Contributor

Initializing a fetch with an incorrect body type throws an error instead rejecting the promise:

 try {
  fetch(API_LOGIN_URL, {
    method: 'POST',
    body: {
      username: 'a',
      password: 'b'
    }
  })
    .catch(error => {
      // not called
      console.error(error)
    })
} catch (e) {
  // called
  console.error(e)
}

Is that standard behaviour? Also why wouldn't the polyfill accept it? Chrome does accept it.

@brentvatne
Copy link
Collaborator

This seems like an issue that is better suited for https://github.com/github/fetch - this is the polyfill that we're using in React Native 😄 Could you repost it there? If it turns out to be a problem with React Native's XMLHttpRequest implementation we can look into that further.

That said, I tried this with some obviously invalid thing - fetching a url that didn't exist, and it didn't go into either of the catch blocks in Chrome.

 try {
  fetch('/lololol', {
    method: 'POST',
    body: {
      username: 'a',
      password: 'b'
    }
  })
    .catch(function(error) {
      // not called
      console.error("INSIDE")
    })
} catch (e) {
  // called
  console.error("OUTSIDE")
}

@despairblue
Copy link
Contributor Author

It's not going into either because it accepts the body init type. That was the second part of my question. Is an Object an officially supported body init type? Because chrome does accept it. 😃

Try this:

 try {
  fetch('/lololol', {
    method: 'POST',
    body: {
      username: 'a',
      password: 'b'
    }
  })
    .then(function(json) {
      console.log(json)
    })
    .catch(function(error) {
      // not called
      console.error("INSIDE")
    })
} catch (e) {
  // called
  console.error("OUTSIDE")
}

Also I'm not completely sure if opening this at github/fetch would help here, since I don't know if fixes from there will be back-ported to react-native's version of fetch. Github made it clear that they are not interested in making the polyfill work anywhere except in a browsers. Also the fetch version in react-native was already patched to work and probably keeps going to get patched.

@sgaurav
Copy link

sgaurav commented Sep 19, 2015

As per https://github.com/github/fetch, to post JSON you need to set correct headers and stringify json before putting in body

fetch('/lololol', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body:JSON.stringify( {
      username: 'a',
      password: 'b'
    })
  })

@shirts
Copy link

shirts commented Dec 30, 2015

Thanks @sgaurav that resolved my issue. Not familiar with the fetch api, and I had forgotten to set headers

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

5 participants