0

I am trying to replicate a cURL request in post request in nodejs.

The cURL:

curl -X POST -H "Authorization: Bearer <TOKEN>" -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data" -F "sampleBase64Content=/9j/4AAQSkZ..." -F "modelId=YCQ4ZACEPJFGXZNRA6ERF3GL5E" https://api.einstein.ai/v2/vision/predict

The REST request in nodejs

var option = {
    url: 'https://api.einstein.ai/v2/vision/predict',
    headers: {
        'Authorization': 'Bearer '+data["access_token"],
        'Cache-Control': 'no-cache',
        'Content-Type': 'multipart/form-data'
    },
    body: 'sampleBase64Content='+imgFile.data.toString('base64')+'&modelId='+modelId
}

request.post(option, function(err, resp, bdy){
    res.send('<span>'+bdy+'</span>');
});

Here, imgFile.data is in buffer format. The response always says bad request. The documentation to the API is here.

1 Answer 1

0

The -F in cURL represents form-data, so I had to modify my request as follows, and it worked fine:

var formData = {
    modelId: modelId,
    sampleBase64Content: imgFIle.data.toString('base64')
}
var option = {
    url: 'https://api.einstein.ai/v2/vision/predict',
    headers: {
        'Authorization': 'Bearer '+data["access_token"],
        'Cache-Control': 'no-cache',
        'Content-Type': 'multipart/form-data'
    },
    formData: formData
}

request.post(option, function(err, resp, bdy){
    res.send('<span>'+bdy+'</span>');
});

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .