Get a pdf file in got.post() #2012
-
|
Is it possible with got.post() to receive a fitxero pdf? I am trying it with this: But the response gives an error because I receive the response as JSON (ParseError: Unexpected token % in JSON ) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
const pdfBuffer = await got.post(url, {
headers: header,
json: bodysuit
}).buffer();
fs.writeFileSync('file.pdf', pdfBuffer);the JSON parsing error happens because by default got tries to parse responses as JSON when you use also, i notice you have |
Beta Was this translation helpful? Give feedback.
pdf: trueisnt a valid got option. to download a PDF (or any binary file), you need to use buffer response type:the JSON parsing error happens because by default got tries to parse responses as JSON when you use
.json()on it, but PDFs are binary data.responseType: 'buffer'tells got to return the raw binary data as a Buffer instead.also, i notice you have
bodysuit- if thats a typo and you meant to send JSON data, use thejsonoption (shown above). if its form data, useforminstead.