【发布时间】:2020-08-18 07:35:22
【问题描述】:
我在 nodejs 中读取通过 curl 请求发送的数据时遇到问题。使用 Postman 一切正常,但是当我尝试通过 curl 发送请求时,api 返回 500 内部服务器错误,我不知道错误在哪里。
这是我的php代码
private function curl($url, $post = false, $header = false)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
}
curl_setopt($ch, CURLOPT_HEADER, [
'Content-Type: application/json',
'Accept: application/json',
'Content-Length: ' . mb_strlen(json_encode($post))
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec ($ch);
curl_close($ch);
return $response;
}
那么现在就跟大家分享一下nodejs的部分
console.log(req.body)
let content = req.body.content
if (!content.length) {
return res.status(401).send({message: 'content is required'})
}
这是日志信息
{ '{"content":"content information"}': '' }
2020-08-18T07:27:38.191100+00:00 app[web.1]:TypeError:无法读取未定义的属性“长度”。 我不知道我的错误在哪里,以及为什么我无法读取节点 js 上的 json 数据,任何帮助都会得到帮助。
【问题讨论】: