2

I am able to get to the server but unable to post my form data. How should I post the form data with the https request? I am using form-data library for form-data, and https request for post call. When I run the following code, I am able to reach the service, but the service gives a response saying that the form data is not submitted.

var https = require('https');
var FormData = require('form-data');
//var querystring = require('querystring');
var fs = require('fs');
var form = new FormData();

connect();

function connect() {
    username = "wr";
    password = "45!"

    var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');
    var options = {
        hostname: 'trans/sun.com',
        port: 443,
        path: '/transfer/upload-v1/file',
        method: 'POST',
        rejectUnauthorized: false,
        headers: {
            'Authorization': auth,
            'Content-Type': 'application/json',
            //'Content-Length': postData.length
        }
    };

    form.append('deviceId', '2612');
    form.append('compressionType', 'Z');
    form.append('file', fs.createReadStream('/Mybugs.txt'));

    var req = https.request(options, function(res) {
        console.log("statusCode: ", res.statusCode);
        //console.log("headers: ", res.headers);
        res.setEncoding('utf8');
        res.on('data', function(d) {
            console.log(d)
        });
    });

    req.write(form + '');
    req.end();

    req.on('error', function(e) {
        console.error(e);
    });
}

1 Answer 1

5

You never link your form to your request. Check this example provided with the form-data documentation

var http = require('http');

var request = http.request({
  method: 'post',
  host: 'example.org',
  path: '/upload',
  headers: form.getHeaders()
});

form.pipe(request);

request.on('response', function(res) {
  console.log(res.statusCode);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.