I'm new to react-native I'm sending a request to server and want to get response and body at same block so that I can send both items to an other function my fetch method is looks like
send_request = (data) =>{
url = BASE_URL + "some/url.json"
fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
user: {
email: data.email,
full_name: data.name,
}
})
}).then((response) => {
//how can I get response body here so that I can call following method
// this.use_response(responsebody, response.headers)
return response.json()
}).then((responseJson) => {
// or how can I get response headers here so that I can call following fuction
// this.use_response(responseJson, headers)
return responseJson
}).catch((error) => {
console.log(error)
});
}
How can I use both at once please help thanks in advance!
The Response interface provides direct methods to access individual header or all the Headers. Simply do a Response followed by a dot ( Response.head ), all the available methods to get headers will be displayed.
A response is defined by its HTTP status code and the data returned in the response body and/or headers. Here is a minimal example: paths: /ping: get: produces: - application/json responses: 200: description: OK.
Request Header is present when you make a request to the server and the response header is present when the server sends a response back to the client/browser. Request Header contains information about the request such as the URL that you have requested, the method (GET, POST, HEAD) ,the browser used to generate the request and other info. Example
Each operation must have at least one response defined, usually a successful response. A response is defined by its HTTP status code and the data returned in the response body and/or headers. Here is a minimal example: An API can respond with various media types.
response.headers
is an object that is available as is, while request.json()
is a promise that needs to be resolved.
In order to get them in one place, with plain ES6 promises, there should be either nested then
s:
...
.then((response) => {
return response.json().then(responseJson => {
this.use_response(responseJson, response.headers)
});
})
Or multiple values should be passed through chain altogether as an array or object:
...
.then((response) => {
return Promise.all([response.json(), response.headers]);
}).then(([responseJson, headers]) => {
this.use_response(responseJson, headers)
})
Or since React application isn't restricted to ES5/ES6 and can use all features that Babel supports, async..await
can be used instead which can solve this kind of problems naturally:
send_request = async (data) =>{
url = BASE_URL + "some/url.json"
const response = await fetch(url, {...})
const responseJson = await response.json();
this.use_response(responseJson, response.headers);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With