When setting the responseType
option to json
and having an afterResponse
hook on the got instance (doesn't matter what it contains) the afterResponse
hook throws an Error when the response from the request is null
This is caused due to a check that happens when running the registered afterResponse
hooks
https://github.com/sindresorhus/got/blob/b1d61c173a681755ac23afb2f155f08801c1e7e4/source/as-promise/index.ts#LL95C2-L95C2
Although 200
responses should have a body in them there are many instances where this behavior is present, and the current "workaround" is to use .json()
and remove the responseType: 'json'
.
This behavior persists even with the resolveBodyOnly: true
option.
Since the proper way of returning these types of requests is with the status code 204
I tried to create it with the same scenario just changing the status code to204
and it seems like got
gets a response but it just hangs at some point. Not sure if it's an issue with fastify
or got
though.
afterResponse
hookresponseType: 'json'
to the requestnull
The 'afterResponse' hook returned an invalid value
afterResponse
hookresponseType: 'json'
to the requestnull
response.body
value of null
import gotTest from 'got'
import fastify from 'fastify'
const app = fastify({
logger: true,
})
await app.register(async (app) => {
app.get('/null-error', async () => {
return null
})
})
await app.listen({port: 8080, host: 'localhost'})
await app.ready()
const got = gotTest.extend({
hooks: {
afterResponse: [(response) => {
return response
}]
}
})
try {
await got.get(`http://localhost:8080/null-error`, {responseType: 'json'})
} catch (error) {
console.log(error)
throw error
}
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too