4

I need to upload a file from my Electron app to a web application (node.js + Express), by first gathering meta-data along with the selected file (from a HTML form in the Electron app) and issue a POST request to the web application. The web application processes this request by saving the file to a pre-configured location on the disk and the meta-data and the path of the saved file to a database. Referring to SO, I decided to use request and form-data to compose the request with the form data and issue a POST request, using code looking like this:

var fs = require('fs');
var request = require('request');
var FormData = require('form-data');

let formData = {
    'name': imageName,
    'mediaType': mediaType,
    'extension': extension,
    'resolution.x': resolutionX,
    'resolution.y': resolutionY,
    'url': url,
    'file': fs.createReadStream(absFilePath)
  };

  request.post({
    url: 'http://localhost:8080/images',
    formData: formData
  }, function(err, response, body) {
    if (err) {
      return console.error('upload failed:', err);
    }
    console.log('Server responded with:', response);
  });

In the Electron app, the console displays Server responded with: [IncomingMessage] and the response status code is 500. It is quite confusing and I am unable to grasp the examples in the Github pages for the two projects. How do I issue a POST request to achieve the above described goals?

Web User
  • 7,438
  • 14
  • 64
  • 92

1 Answers1

0
npm install -S request eyespect

.

var inspect = require('eyespect').inspector();
var request = require('request')

var postData = {
  name: 'test',
  value: 'test'
}

var url = 'https://www.example.com'
var options = {
  method: 'post',
  body: postData,
  json: true,
  url: url
}
request(options, function (err, res, body) {
  if (err) {
    inspect(err, 'error posting json')
    return
  }
  var headers = res.headers
  var statusCode = res.statusCode
  inspect(headers, 'headers')
  inspect(statusCode, 'statusCode')
  inspect(body, 'body')
})