Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get response body and response headers in one block

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!

like image 917
Asnad Atta Avatar asked Sep 10 '18 07:09

Asnad Atta


People also ask

How to get all the headers of a response object?

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.

What is a a response in http?

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.

What is the difference between request header and response header?

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

How many HTTP responses are required for each operation?

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.


1 Answers

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 thens:

  ...
  .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);
}
like image 134
Estus Flask Avatar answered Oct 07 '22 02:10

Estus Flask