Benjamin79
Benjamin79

Reputation: 190

curl request with node.js

I´d like to get the following curl working in node.js

curl -F source=@"pedestrian.png" -F model="pedestrian" localhost:3350/dpm/detect.objects

This is what i have with the use of require('request'):

 request('http://localhost:3350/dpm/detect.objects', function (error,    response, body) {
  console.log(response.statusCode);
  if (!error && response.statusCode == 201) {
    console.log(body) 
  }
 })

How can i translate the curl command in node.js? It doesn´t have to be the request library if there is a better solution.

Upvotes: 0

Views: 831

Answers (1)

zaynetro
zaynetro

Reputation: 2308

Take a look at documentation. You need to pass a formData object.

var formData = {
  source: fs.createReadStream(__dirname + '/pedestrian.jpg'),
  model: 'pedestrian'
};

request.post({ 
    url:'http://localhost:3350/dpm/detect.objects', 
    formData: formData 
  }, function (err, httpResponse, body) {
    if (err) {
      return console.error('upload failed:', err);
    }
    console.log('Upload successful!  Server responded with:', body);
  }
);

Upvotes: 4

Related Questions