2

I'm using the request-promise-native library, and I'm trying to get the full response from a request instead of just body because there is something in the response header I need to log. I know usually to accomplish that task you would do something like so:

request.post(opts).then(function (response) { //response }).catch(function (err) { // error })

but I'm using Async/await, so my code looks something like this: const data = await request.post(opts), however, this will only return the body of the response I want access to the header as well, how would I go about doing that?

2 Answers2

1

You can use fetch :

 const response = await fetch(URL, {
                                   method: 'POST',
                                   headers: {
                                             'Accept': 'application/json',
                                             'Content-Type': 'application/json',
                                            }
                             })

And then :

const fromHeader = JSON.parse(response.headers.get('header-name'))
sagi
  • 40,026
  • 6
  • 59
  • 84
0

I used the resolveWithFullResponse option with Request and that solved the issue. e.g resolveWithFullResponse: true