curl 在内容类型上发送空 JSON:application/json



我正在使用带有POST方法的curl将json格式的CPU和内存使用情况从Ubuntu发送到Node.js API。但是,在 Node.js 服务器上,json 数据是空的。

Ubuntu 上的 Bash 脚本

top -b -n 2 -d 0.5 > top.txt
cpu_i=$(grep Cpu top.txt | cut -d ',' -f 4 | cut -d ' ' -f 2)
cpu=$(echo $cpu_i | cut -d ' ' -f 2)
echo $cpu
mem=$(grep "KiB Mem :" top.txt | cut -d ':' -f 2)
#echo $mem
mem_used=$(echo $mem | cut -d ',' -f 3 | cut -d ' ' -f 2)
echo $mem_used
curl --header "Content-Type: application/json" -d "{"cpu":"$cpu", "memory":"$mem_used","device":"ubuntu"}" http://192.168.10.10:4000/collector

节点.js服务器上的输出

{} 远程地址:::ffff:192.168.10.5

如果我没记错的话,默认情况下,httpverb 设置为get。如果要发布它,请使用-X POST。这可能会解决您的问题,因为 curl 命令是可以的。

curl -X POST --header "Content-Type: application/json" -d "{"cpu":"$cpu", "memory":"$mem_used","device":"ubuntu"}" http://192.168.10.10:4000/collector

相关内容

最新更新