expressjs POST header parameters?
I'm not posting to the body, and the key/value pairs do not show up anywhere I've looked when doing a curl post from PHP -- where are they?!
Update:
Using http_build_query($parameters) in the curl post worked, as they then became available in req.body, but it just seems odd that sinatra, play, and other web api frameworks all handle the posted array the same (w/o the http_build_query method), and express seems like the bastard stepchild. Whom is in the right?
The app.js has config:
app.use(express.bodyParser());
app.use(express.methodOverride());
The call:
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));
});
The php looks like:
$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_RETURNTR开发者_如何学编程ANSFER, 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;
}
bodyParser fills Request.body
from POST request parameters only if header Content-type
of the request has value application/x-www-form-urlencoded
. If request has no paramenters or different content type, Request.body object will be undefined.
精彩评论