我正在尝试使用shell脚本中的curl命令发送post请求。
当我在终端中运行这个curl命令时,它工作得很好。
curl -d '{"DataArr":[{"serialno":"ab1","filePath":"/home/name/notes1"}]}' -H 'Content-Type: application/json' -H 'Authorization: tasfdgfdhh...' http://localhost:8081/create 同样,我试图将其作为shell脚本文件运行,它正在命中服务器,但数据格式不是预期的。数组括号被转换为:“或者类似于:{。我尝试了很多次不同的可能性,但我不知道如何准备正确的请求。以下是服务器端的代码和响应。
我使用循环语句来模拟多个请求,
code:
#!/bin/bash
echo "hello welcome to testing";
token=asdghadjakalskl.....
requestNo=10
i=0
sNo=(ab1 ab2 ab3 ab4 ab5)
fn=(notes1 Pictures/crop.jpeg Pictures/cat.jpeg Pictures/mountain.jpg Pictures/magic.jpeg Pictures/tree.jpg)
echo $1
for (( i=0; i< $1;i++ ))
do
# echo "welcome inside the loop $s"
echo ${sNo[i%5]} ${fn[i%6]}
f="/home/name/${fn[i%6]}"
# echo $token
echo file $f
echo $i request sent
curl -d '{"DataArr":[{"serialno":'${sNo[i%5]}',"filePath":'$f'}]}' -H "'Content-Type: application/json'" -H 'Authorization: '$token'' http://localhost:8080/create
done在服务器端,
{"{\"qualityDataArr\":":{"{\"serialNumberCustomer\":ab1,\"filePath\":/home/name/notes1}":""}}请给我引路。谢谢!
发布于 2021-10-14 15:58:35
尝试echo """'{\"DataArr\":[{\"serialno\":\"${sNo[i%5]}\",\"filePath\":\"$f\"}]}'""",如果显示良好,则使用echo curl -d """'{\"DataArr\":[{\"serialno\":\"${sNo[i%5]}\",\"filePath\":\"$f\"}]}'""" -H "'Content-Type: application/json'" -H """'Authorization: $token'""" http://localhost:8080/create | bash
https://stackoverflow.com/questions/69573427
复制相似问题