javascript - Unable to read octet stream response in node js? - Stack Overflow

I have an API that returns response as 'octet-stream'. Here is swagger defination for the sam

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 of response.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
 |  Show 1 more ment

1 Answer 1

Reset to default 3

I 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信