boy_v
boy_v

Reputation: 627

How can i get response from fetch API React Native

{  
   "success":true,
   "result":{  
      "sessionName":"2a7777703f6f219d"
      "userId":"19x1"
      "version":"0.22"
   }
};

fetch('https://myapi/api', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    firstParam: 'xxxx1',
    secondParam: 'xxxx1',
  })
})

How can i get response from fetch API React Native (Post method)

I need to console.log to see sessionName in result (I think it's response.result.sessionName)

But I don't know how to get it from fetch.

Where is response from fetch like a get method


Here's Get method from facebook (it's have response)

 function getMoviesFromApiAsync() {
    return fetch('https://facebook.github.io/react-native/movies.json')
      .then((response) => response.json())
      .then((responseJson) => {
        return responseJson.movies;
      })
      .catch((error) => {
        console.error(error);
      });
  }

Upvotes: 0

Views: 4262

Answers (1)

thinhvo0108
thinhvo0108

Reputation: 2232

You can just do the same as GET method:

fetch('https://myapi/api', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    firstParam: 'xxxx1',
    secondParam: 'xxxx1',
  })
})
.then((response) => response.json())
.then((responseJson) => {
    console.log(responseJson);// your JSON response is here
})
.catch((error) => {
    console.log(error);
});

Upvotes: 1

Related Questions