bash - curl 在 content-type: application/json 上发送空 json
问题描述
我使用 curl 和 POST 方法将 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
Node.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
推荐阅读
- c++ - 为什么编译器会抱怨“std::vector
诉(10);对于 (auto& e : v){}" 而 "std::vector 诉(10);for (auto& e : v){}" 编译? - splunk - Splunk中如何计算field和gropuby field的比值
- node.js - Mongo 投影查询结果与 shell 与 nodejs 应用程序中的测试不同
- python - 为什么我可以调用从原始数据到 PCA numpy.ndarray 绘图的键?
- python - 为什么 tensorflow dll 没有运行?
- .net - 条纹支付 CardInputWidget 更改输入的字体颜色
- entity-framework - EF Core:同时进行多个 Where 查询
- python - 使用 Lambda 函数和变量参数求和
- python-3.x - 是否可以更改 np.add() 函数的精度?
- r - 使用 str_detect 提取字符串的值