How to throw Catch in fetch POST request in react native

Kasper Niedrig

I am having a problem with calling an error when i call a POST request in React Native, i know that there is a catch but it only throws for network connections, how do i do that with errors i get from the response when i have a wrong password or wrong username like when this Error is called from the Response:

Object {
  "error": "invalid_request",
  "error_description": "Missing form parameter: grant_type",
}
    fetch("https://websiteo.com/auth/realms/realmsid/protocol/openid-connect/token", {
      method: 'POST',
      headers: myHeaders,
      body: urlencoded.toString(),
    })
      .then(response => response.json())
      .then(result => {
        //console.log(result)
        console.log(result)
        saveToken('secure_access_token', `${result.access_token}`)
        props.navigation.navigate('HomeApp') //i need to throw catch also when username or password wrong
      })
      .catch(error => alert(error.response.data)) //This is only thrown when network problems 

Jose Rojas

You can verify in response json if was 200 or failed, something like this:

fetch("https://websiteo.com/auth/realms/realmsid/protocol/openid-connect/token", {
  method: 'POST',
  headers: myHeaders,
  body: urlencoded.toString(),
})
  .then(response => {
     if (response.ok) {
        return response.json();
     } else {
        throw new Error('Something went wrong');
     }
  })
  .then(result => {
    //console.log(result)
    console.log(result)
    saveToken('secure_access_token', `${result.access_token}`)
    props.navigation.navigate('HomeApp')
  })
  .catch(error => alert(error.response.data)) //This is only thrown when network problems 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React Native fetch() Network Request Failed

React Native fetch() Network Request Failed

React Native Post Request via Fetch throws Network Request Failed

How to post a form using fetch in react native?

React Native Fetch Return Network Request Failed

How can I pass POST parameters in fetch request? - React Native

REACT fetch post request

React Native fetch() Network Request Failed for android?

How to post multipart/formdata using fetch in react-native?

Disable redirect in fetch request using React native

React Native: Use component state for a fetch request

Cancel a fetch request in react-native

React native fetch request failed

How to send a fetch post request from react component to backend

how to fetch api(POST) with header in react native app

How to display json data on ListView react native by post request api

React_Native Using Post Request with Fetch isn't work(PHP is work normaly)

React native fetch api call is not making the request

React Native fetch request to Microsoft Azure failing

react native fetch http request throw error

React Native Fetch Request Network Error

.catch() in react-native's fetch

React - how to Redirect path after completing POST request using fetch

React - PHP: How to fix problem of CORS with fetch request for POST request?

Fetch request fails on expo on react native on android

How to send a POST request to Flask API using Fetch in a React app

How can I make a POST request in react native

How to properly catch fetch request in django

How to output fetch request response in react native

TOP Ranking

HotTag

Archive