javascript - How to send form data in node.js -
i able server unable post form data. how should post form data https request? using form-data library form-data, , https request post call. when run following code, able reach service, service gives response saying form data 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); }); }
you never link form request. check example provided 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); });
Comments
Post a Comment