[英]Simple Post formData with node.js and request keep failing
我正在嘗試使用node.js和請求模塊將帶有文件的數據發布到遠程服務器,代碼發送沒有文件的數據。 我使用文檔示例https://github.com/request/request#forms
我可以使用下面的代碼通過php curl發布相同的數據,因此我知道遠程服務器代碼有效。
我究竟做錯了什么?
$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);
我的node.js代碼
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) {
});
此代碼成功發布了文件。
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.