我不会发布到正文,并且在从 PHP 进行curl 发布时,键/值对不会出现在我看过的任何地方 - 它们在哪里?!
更新:
在curl帖子中使用http_build_query($parameters)是有效的,因为它们随后在req.body中可用,但奇怪的是sinatra、play和其他Web api框架都以相同的方式处理发布的数组(没有http_build_query方法),而express似乎是 SCSS 继子。右边是谁?
app.js 有配置:
app.use(express.bodyParser());
app.use(express.methodOverride());
通话:
app.post('/call/:genericUrlParam', function(req, res){
// where is 'key'/'value' pair?!
console.log('headers: ' + JSON.stringify(req.headers));
console.log('body: ' + JSON.stringify(req.body));
});
PHP 看起来像:
$parameters = array (
'key' => 'value'
);
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "localhost/call/myparam");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 50);
curl_setopt($curl_handle, CURLOPT_USERPWD, "username:password");
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $parameters);
$buffer = curl_exec($curl_handle);
$error = curl_error($curl_handle);
curl_close($curl_handle);
if (empty($buffer)) {
return "Error: ".$error;
} else {
return $buffer;
}
最佳答案
仅当请求的 header Content-type
值为 application/x-www-form-urlencoded
时,
bodyParser 才会从 POST 请求参数填充 Request.body
。如果请求没有参数或不同的内容类型,Request.body 对象将是未定义的。
关于javascript - expressjs POST header 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7213427/