I am trying to post data with a file to remote server using node.js and request module, the code send the data without the file. I use the docs example https://github.com/request/request#forms
I can post the same data with php curl with the code below , so I know the remote server code works.
What am I doing wrong?
$post = array(
'sessionId' => 1234,
'source' => 'text',
'files[jpg]' => '@' . $file_name_with_full_path
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
My node.js code
var fs = require("fs");
var formData = {
sessionId: 1234,
source: 'text',
files: JSON.stringify (
{jpg: fs.createReadStream( __dirname +"/"+ file.txt)}
)
request.post({url: 'https://url', formData: formData},
function optionalCallback(err, httpResponse, body) {
});
This code successfully posted the file.
var r = request.post('https://url', function optionalCallback(err,httpResponse, body) { })
var form = r.form();
form.append('sessionId', 1234);
form.append('source', 'text');
form.append('file[jpg]', fs.createReadStream( __dirname +"/"+ file.txt);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.