0

I am using node.js, axios, an fs. I am hitting the vanceai.com api. And when I write the file I am getting a bad file. Code:

export const imageDownloader = async (transId, name, ext) => {
    let data = qs.stringify({
        'api_token': process.env.VANCE_AI_API_KEY,
        'trans_id': transId 
    });
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: 'https://api-service.vanceai.com/web_api/v1/download',
        headers: { 
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data : data
    };

    await axios.request(config).then((response) => {
        let buff = Buffer.from(JSON.stringify(response.data));
        fs.writeFile(`./finalImages/${name}.${ext}`, buff, () => console.log('finished downloading!'));
    }).catch((error) => {
        console.log(error);
    });


}

Res file is to long but here is some of it.

"����\u0000\u0010JFIF\u0000\u0001\u0001\u0000\u0000\u0001\u0000\u0001\u0000\u0000��\u0000C\u0000\u0002\u0001\u0001\u0001\u0001\u0001\u0002\u0001\u0001\u0001\u0002\u0002\u0002\u0002\u0002\u0004\u0003\u0002\u0002\u0002\u0002\u0005\u0004\u0004\u0003\u0004\u0006\u0005\u0006\u0006\u0006\u0005\u0006\u0006\u0006\u0007\t\b\u0006\u0007\t\u0007\u0006\u0006\b\u000b\b\t\n\n\n\n\n\u0006\b\u000b\f\u000b\n\f\t\n\n\n��\u0000C\u0001\u0002\u0002\u0002\u0002\u0002\u0002\u0005\u0003\u0003\u0005\n\u0007\u0006\u0007\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n��\u0000\u0011\b\u0002�\u0005\u0010\u0003\u0001\"\u0000\u0002\u0011\u0001\u0003\u0011\u0001��\u0000\u001f\u0000\u0000\u0001\u0005\u0001\u0001\u0001\u0001\u0001\u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b��\u0000�\u0010\u0000\u0002\u0001\u0003\u0003\u0002\u0004\u0003\u0005\u0005\u0004\u0004\u0000\u0000\u0001}\u0001\u0002\u0003\u0000\u0004\u0011\u0005\u0012!1A\u0006\u0013Qa\u0007\"q\u00142���\b#B��\u0015R��$3br�\t\n\u0016\u0017\u0018\u0019\u001a%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������������������������������������������\u0000\u001f\u0001\u0000\u0003\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b��\u0000�\u0011\u0000\u0002\u0001\u0002\u0004\u0004\u0003\u0004\u0007\u0005\u0004\u0004\u0000\u0001\u0002w\u0000\u0001\u0002\u0003\u0011\u0004\u0005!1\u0006\u0012AQ\u0007aq\u0013\"2�\b\u0014B����\t#3R�\u0015br�\n\u0016$4�%�\u0017\u0018\u0019\u001a&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������\u0000\f\u0003\u0001\u0000\u0002\u0011\u0003\u0011\u0000?\u0000��a��\u001cT��� \u0003֭�j����c��׆�G�Z�\u001c? \u0014�\u0013\u0006�q@�pEM\u0015�NJ�N�h��9\u001cP֏�\"�c� }ڐ[�u��'�΂��ȵ8\u0004zո�\\�h�8Z��\t\u0015�a����\u0013l㰨�G��O_J������&٣Z\r\u0011��'� �\u00079�70�\u000f�)˼���I\b�c�i\u001c0\u0018�A`8�I��T\u0016BP�\u001f�V�\u0002�\u001c�\u0015l�=\u0001�ң`\u000br��\u0004[Q��|ۇ�R:\u001c�\u001d)��S�R\u001eO\u0002�{\u0002�\"��x��H���Q��i29@\u0002i�)�H��\b��\u001d�\u001aJ�\u001cI\u0014|����bs�Vg8\u0018?�(\f@��TӸZĶ͊�85\b�:\u0003�R+8�x����\u001c����˷�5\f\u0003-�\u0013銐��\u0006��\u0005�\u000eE;sc<S@'���c�H��G5V�\u00101S�Y{Tn\u001b8�\u0017\u0018�\u0003�\u001a{\u0006\u0007\u0000�+\u0010@\nj@�~�4\u0000�RE#���H\u0014�4�\f\u000eE\u0001�\u001e29�:\u001e��[<\nk\u0013Ԋw\u0001�I\\�epz\u001cS˾9S�P%\u0000m*h���\u0019�N\u0014\u001al�A\u000bR\t\bo�֗%�\u0019搤$

I have tried many things, any help would be appreciated.

I have tried to duplicate this with a curl request using https://www.npmjs.com/package/node-libcurl. Their API says to do the following but I can not duplicate

curl -X POST "https://api-service.vanceai.com/web_api/v1/download" \
     -o /Users/vanceai/Downloads/enlarged-cat.jpg \
     -d 'api_token=1234567890abcdefg' \
     -d 'trans_id=6de4b562d1a01c3d2520608eae929646' \

note: my response log looks just like what I am getting out of postman.

  • Not a great answer but I ended up doing this with npm shelljs export const imageDownloader = async (transId, name, imageType) => { shell.exec(\`curl -X POST "https://api-service.vanceai.com/web_api/v1/download" \ -o ./finalImages/${name}.${imageType}\ -d 'api_token=${process.env.VANCE_AI_API_KEY}' \ -d 'trans_id=${transId}' \`); } – Joe Whitehorn Jul 21 '23 at 21:12

1 Answers1

0

The important part is setting the responseType in axios.

import { writeFile } from "fs/promises";
import { createWriteStream } from "fs";
import axios from "axios";

const response = await axios.get("https://www.google.com/images/branding/googlelogo/1x/googlelogo_light_color_272x92dp.png",
    { responseType: "stream" } // alternatively "arraybuffer" would also work with writeFile()
  );
await writeFile("./test.jpg", response.data);

// when using the responseType "stream" you could also write the file using
// response.data.pipe(createWriteStream("./test.jpg"));
Mushroomator
  • 6,516
  • 1
  • 10
  • 27