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) {
});