I'm trying to return a cached response in a beforeRequest hook (2nd example in doc: advanced caching mechanism) but using a FormData as request body (2nd example in doc: options > body).
Got seems to be failing silently without printing "result 3" from the code below.
Got should return cached data after "result 3" from the code below.
import got from 'got'
import { FormData } from 'formdata-node'
import { Readable } from 'node:stream'
const getCachedResponse = (url, options) => {
const response = new Readable({
read() {
this.push('{"data": "cached-json-output"}')
this.push(null)
}
});
response.statusCode = 200
response.headers = {}
response.trailers = {}
response.socket = null
response.aborted = false
response.complete = true
response.httpVersion = '1.1'
response.httpVersionMinor = 1
response.httpVersionMajor = 1
return response
};
const instance = got.extend({
hooks: {
beforeRequest: [
options => {
return getCachedResponse(options.url, options)
}
]
}
})
const form = new FormData()
form.set('data', 'formdata-input')
// Working example without beforeRequest hook
const data1 = await got.post('https://httpbin.org/post', {
body: form
}).json()
console.log('result 1: ', data1)
// Working example without form-data body
const data2 = await instance.post('https://httpbin.org/post', {
body: '{"data": "json-input"}'
}).json()
console.log('result 2: ', data2)
// Failing example with beforeRequest hook and form-data body
const data3 = await instance.post('https://httpbin.org/post', {
body: form
}).json()
// This console.log is never shown
console.log('result 3: ', data3)
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