作者:玛丽 | 来源:互联网 | 2023-12-10 10:19
本文讨论了在使用PHPcURL发送POST请求时,请求体在node.js中没有定义的问题。作者尝试了多种解决方案,但仍然无法解决该问题。同时提供了当前PHP代码示例。
I've tried all the examples on these SO posts:
我已经尝试了所有的例子,所以贴子:
How do I send a POST request with PHP?
如何使用PHP发送POST请求?
PHP cURL Post request not working
PHP cURL Post请求不工作
Always my request.body is undefined
yet in the request itself I see "_hasBody":true
总是我的请求。body尚未定义,我在请求本身中看到“_hasBody”:true
The current code for my php post file:
我的php post文件的当前代码:
function httpPost($url,$data){
$curl = curl_init($url);
curl_setopt($curl,CURLOPT_POST,true);
curl_setopt($curl,CURLOPT_POSTFIELDS,http_build_query($data));
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
$respOnse=curl_exec($curl);
curl_close($curl);
return $response;
}
$fields = array(
'name' => 'ben'
, 'foo' => 'bar'
);
echo httpPost("http://localhost:8002", $fields);
Then my node.js listening server code is:
然后我的节点。js监听服务器代码为:
var test=require('http').createServer(function(q,a){//question,answer
console.log(q.body);
console.log(JSON.stringify(q).indexOf('ben'));
a.end(JSON.stringify(q));
});
test.listen(8002,function(e,r){console.log("listening");});
As you can see, in the node.js server I search the request for my name but the console says
可以看到,在节点中。我在请求中搜索我的名字,但是控制台显示
undefined//no body
-1//could not find your name in the request
then I hand over the request back to the response and print it to the page so I can see the whole data.
然后我将请求返回到响应,并将其打印到页面,以便查看整个数据。
logically it would seem that I am doing the cURL part right as its copied code, so I would say I might be doing something wrong to access the vars
从逻辑上看,我似乎把cURL部分作为它的,所以我认为我可能在访问vars时做错了什么
My question is how do I see the request body or where the vars?
我的问题是,我如何看到请求体或vars?
1 个解决方案