I have an API that returns response as 'octet-stream'. Here is swagger defination for the same.
responses: {
'200': {
description: 'Will return the pdf for an invoice',
content: {
'application/octet-stream': {
schema: '',
},
},
},
}
From postman and swagger ui i am able to save the response as a PDF File. but with node i am not able to write the pdf file.
Below is code for node js to call the API.
var fs = require('fs');
var request = require('request');
let headers = {
'Content-Type': 'application/json',
'accept': "application/octet-stream",
}
let body = {
"invoiceId": "343",
"slCompId": 243,
"platfromCompId": "4620816365013235830"
}
request.post({
headers,
url: 'http://localhost:3001/invoice/',
json: body
},
function (error, response, body) {
console.log(response);
console.log("response");
fs.writeFile('a.pdf', response.body, 'binary')
});
EDIT:
The PDF file which is written is corrupted. There is nothing inside the file and PDF viewer gives me error in opening the file.
I have an API that returns response as 'octet-stream'. Here is swagger defination for the same.
responses: {
'200': {
description: 'Will return the pdf for an invoice',
content: {
'application/octet-stream': {
schema: '',
},
},
},
}
From postman and swagger ui i am able to save the response as a PDF File. but with node i am not able to write the pdf file.
Below is code for node js to call the API.
var fs = require('fs');
var request = require('request');
let headers = {
'Content-Type': 'application/json',
'accept': "application/octet-stream",
}
let body = {
"invoiceId": "343",
"slCompId": 243,
"platfromCompId": "4620816365013235830"
}
request.post({
headers,
url: 'http://localhost:3001/invoice/',
json: body
},
function (error, response, body) {
console.log(response);
console.log("response");
fs.writeFile('a.pdf', response.body, 'binary')
});
EDIT:
The PDF file which is written is corrupted. There is nothing inside the file and PDF viewer gives me error in opening the file.
Share Improve this question edited Feb 11, 2020 at 5:13 TechChain asked Feb 10, 2020 at 4:21 TechChainTechChain 8,96032 gold badges117 silver badges244 bronze badges 6- What does "i am not able to write the pdf file" mean? – Andy Ray Commented Feb 10, 2020 at 4:32
- I get response for writing the PDF file. I am not able to write. – TechChain Commented Feb 10, 2020 at 9:40
- What does "I am not able to write" mean? Are you getting an error? Is the file created but blank? It helps to be specific on Stackoverflow. Also, you're not passing the callback to writeFile, so it may be erroring and something amy be eating the error nodejs/api/… – Andy Ray Commented Feb 10, 2020 at 17:22
- The file which is written is corrupted. There is nothing inside the file and PDF viewer gives me error in opening the file. – TechChain Commented Feb 11, 2020 at 5:12
-
did you try writing
body
instead ofresponse.body
? Also, you could use streams instead of buffering the whole file first: github./request/request#streaming. Additionally,request
is deprecated as of today, so you're probably fine to use superagent but the point about streaming still stands. – gcochard Commented Feb 12, 2020 at 4:47
1 Answer
Reset to default 3I have found a solution to this problem. I tried hitting the API with superagent with buffer
as true
.
async function getData() {
try {
var fs = require('fs');
let res = await superagent
.get('https://localhost:3000/invoice')
.set("Content-Type", "application/json")
.set("accept", "application/octet-stream")
.buffer(true).disableTLSCerts()
console.log(res)
fs.writeFile('a.pdf',res.body)
}
catch(error) {
console.log("error " + error)
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745329254a4622800.html
评论列表(0条)