Got: How to get error response body in beforeRetry hook ?

Created on 26 Aug 2020  Â·  5Comments  Â·  Source: sindresorhus/got

How to get error response body in beforeRetry hook ?

i am getting error.response.statusCode fine but not getting error.response.body ?

I have read the documentation. no mention of how to get error body ?

sample code to generate the issue

const got = require("got")

got(errorUrl, {
  hooks: {
    beforeRetry: [
      (options, error, retryCount) => {
       console.log(error.response.statusCode) //408 
        console.log(error.response.body) // undefined  [issue]  Expected : this is 408 error body
      },
    ],
  },
})

the php code i used to generate error is below errorUrl.

<?php header('HTTP/1.1 408'); echo 'this is 408 error body'; ?>

bug regression ✭ help wanted ✭

Most helpful comment

It seems to be fixed in the master branch since commit https://github.com/sindresorhus/got/commit/707219825b2f8ff3df597230a53244636df21642

All 5 comments

Reproducible:

const got = require("got");
const server = require('http').createServer((request, response) => {
    response.statusCode = 500;
    response.end('nope');
});

server.listen(() => {
   got(`http://localhost:${server.address().port}`, {
      hooks: {
        beforeRetry: [
          (options, error, retryCount) => {
           console.log(error.response.statusCode) //408 
            console.log(error.response.body) // undefined  [issue]  Expected : this is 408 error body
          },
        ],
      },
    }).catch(() => {}); 
});

Is this happening with a specific version of Node or the Library?

When I cloned down got added the reproducible code into a test.js file and ran it I got nope back for error.response.body

Screen Shot 2020-08-27 at 5 45 39 PM

hi,
my version is same "got": "11.5.2", node v12.10.0, os windows 10 64 bit

i am not getting body?
image

It seems to be fixed in the master branch since commit https://github.com/sindresorhus/got/commit/707219825b2f8ff3df597230a53244636df21642

Ah yes, the _beforeError is removed for Promise API and therefore it defines the body here:

https://github.com/sindresorhus/got/blob/b134aa488a3d903d9571914b0f8b267340b767a9/source/core/index.ts#L2481

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AxelTerizaki picture AxelTerizaki  Â·  3Comments

carvallegro picture carvallegro  Â·  4Comments

erfanium picture erfanium  Â·  3Comments

dominusmars picture dominusmars  Â·  3Comments

tkoelpin picture tkoelpin  Â·  3Comments