Home > Net >  curl --form image as axios?
curl --form image as axios?

Time:12-25

I need to convert this curl to axios.

url -X POST "https://apis.aligo.in/send/" \
    --data-urlencode "key=xxxxx" \
    --data-urlencode "user_id=xxxxx" \ \
    --data-urlencode "rdate=20211222" \
    --form image=@localfilename

Ok, So I made this with nodejs.

 axios.post(url, null, { 
   key: ***, user_id: ***, rdate: ***
 })

Everything is working fine, but I don't know how to convert the image part that curl says.

--form image=@localfilename

futher more i don't wanna put local image i got only URL

const myimg = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/481px-Cat03.jpg"

Can i just download it and change to blob and send it ? Could you write an example?

CodePudding user response:

The way I see it, you need to download an image with Axios, and then upload that same image at the same time. There are plenty of examples out there.

So, you need to get the image somehow. Alternative to downloading it would be to store it to the memory. Axios supports blob only in browser environment, so you could try using ArrayBuffer to store the image, by setting the responseType to arraybuffer (for larger files stream should be better, but that would require having a file to save the stream to).

After getting the image as an ArrayBuffer, it needs to be encoded as a string to allow including into JSON object, which will then be passed to Axios as a request body (and read on the server handling the upload, so reading the image on the server should require decoding the image string).

Here's the code:

const axios = require('axios');

async function getImage() {

    // image url
    const imageUrl = 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/481px-Cat03.jpg';

    // get the image from the url
    let imageArrayBuffer;

    try {

        imageArrayBuffer = await axios({
            method: 'GET',
            url: imageUrl,

            // from the docs: https://github.com/axios/axios
            // `responseType` indicates the type of data that the server will respond with
            // options are: 'arraybuffer', 'document', 'json', 'text', 'stream'
            //   browser only: 'blob'
            responseType: 'arraybuffer'

        });

    } catch (err) {
        console.error('error getting image', err);
        // handleErrorSomewhere();
        return;
    }

    // now upload the image
    // to be able to include ArrayBuffer into a JSON, decode the buffer and encode image as string
    const image = Buffer.from(imageArrayBuffer.data, 'binary').toString('base64');

    // setup your upload url
    const uploadUrl = 'https://apis.aligo.in/send/';

    // prepare the body
    const body = {
        key: '',
        user_id: '',
        rdate: '',
        // string image
        image,
    };

    // upload the image and the rest
    axios.post(uploadUrl, body);

}

// run the thing
getImage()

and on the server (Node.js with Express):

router.post('/send', (req, res) => {

    // decode image
    const image = new Buffer.from(req.body.image, 'base64');

    const fs = require('fs');

    const image = new Buffer.from(req.body.image, 'base64');

    fs.writeFile('image.jpg', image  , (err) => {
        console.log('image saved');
        res.status(200).json({ message: 'Thank you' });
    });
  • Related