【发布时间】:2017-03-11 20:48:50
【问题描述】:
设置
我在以下 bash 脚本中使用 curl 将 JSON 文件推送到在 nginx 后面的 Tomcat 中运行的 REST API。
while IFS= read -d '' -r file; do
base=$(basename "$file")
datetime=$(find $file -maxdepth 0 -printf "%TY/%Tm/%Td %TH:%TM:%.2TS")
curl -vX POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" \
-d @"$file" -u vangeeij:eian12 \
"http://192.168.105.10/homeaccess/services/aCStats/uploadData?username=vangeeij&filename=$base&datetime=$datetime"
#sudo mv "$file" /home/vangeeij/acserver/resultsOld
done < <(sudo find . -type f -print0)
问题
运行此脚本时,我收到带有 curl 错误的 http 400 响应:
curl: (18) transfer closed with outstanding read data remaining
我的尝试
我发现了两件事。首先通过 Postman 运行相同的 URL 和正文会产生一个成功的 POST。
我发现从 URL &datetime=$datetime 中删除最后一个参数后,此错误消失了
我还发现了此错误与设置 curl 选项之间的一些联系,例如
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
但我不确定在简单的 bash 脚本中使用 curl 时在哪里/如何设置它
问题
我需要在 curl 命令中进行哪些更改才能消除错误并仍然能够使用所有参数?
更新
开始一个新问题,因为进一步的调查使我对问题有了更好的理解。
该错误与参数 datetime= 最终包含需要 URL 编码的文本有关。
已通过将变量替换为 2017%2F03%2F01%2008%3A50%3A56
它成功了。
所以现在的问题是,我无法让 --data-urlencode datetime=$datetime 工作。似乎这只是附加到 JSON 数据或其他东西上。
【问题讨论】:
-
我会尝试删除这一行中的 '\n'。 datetime=$(find $file -maxdepth 0 -printf "%TY/%Tm/%Td %TH:%TM:%.2TS\n")。您也可以添加 -d "username=vangeeij&filename=$base&datetime=$datetime" 并将其从 URL 中删除,因为 curl 合并多个 -d
-
我没有注意到那里的 \n ..我删除了那个...但错误仍然存在。更新上述内容以反映这一点。