express: axios response data incorrect

Was hoping i can get some assistance on this. I am calling another API which returned an attachment to me. If i was to call that API normally by using postman or anything i can download the ZIP successfully and unzip the contents. But when i implement it to express my data is not right.

As seen below the issue is because when i try writing the returned data to a file it is not right. The content-length is actually supposed to be 24109 but when i look at the response data length i only have 23062. Is this because of some sort of body parser that i need to implement?

I have the following code:

const express = require('express')
const axios = require('axios');
const app = express()

const generate = () => {
  return axios.post('http://localhost:8050/api/json', {
      host: 'lightapi.net',
      service: 'codegen',
      action: "generate",
      version: '0.0.1',
      data: [{"framework":"swagger"}]
  }, {
      responseType: 'blob',
  });
};

app.get('/', (req, res) => {
  generate().then((response) => {
    console.log('########### HEADERS: ', response.headers);
    console.log('########### AXIOS: ', response.data.length);
  });
  res.send('Hello World!');
});

app.listen(9000, () => console.log('Example app listening on port 9000!'))

and i get the following response:

########### HEADERS:  { connection: 'close',
  server: 'L',
  'content-type': 'application/zip',
  'content-length': '24109',
  'content-disposition': 'attachment',
  date: 'Fri, 27 Apr 2018 16:48:05 GMT' }
########### AXIOS:  23062

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 17 (9 by maintainers)

Most upvoted comments

So it may help here, but I have no idea how axios works, so cannot say what this does or if this is right. If I change responseType: 'blob' to responseType: 'arraybuffer' I see both report the same 24109 value.