bash curl 错误 18 传输已关闭,剩余未完成的读取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42740461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
curl error 18 transfer closed with outstanding read data remaining
提问by DarbyM
Setup
设置
I'm Using curl in the following bash script to push a JSON file to a REST API running in tomcat sitting behind nginx.
我在以下 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)
Problem
问题
When running this script I get a http 400 response with curl error:
运行此脚本时,我收到带有 curl 错误的 http 400 响应:
curl: (18) transfer closed with outstanding read data remaining
What I have tried
我试过的
I have found 2 things. First running the same URL and body through Postman yields a successful POST.
我发现了两件事。首先通过 Postman 运行相同的 URL 和正文会产生一个成功的 POST。
I found that this error goes away when the last parameter is removed from the URL &datetime=$datetime
我发现当从 URL 中删除最后一个参数时,此错误消失了 &datetime=$datetime
I have also found a few connections between this error and setting a curl option something like
我还发现了此错误与设置 curl 选项之间的一些联系,例如
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
But I'm not sure where/how to set this exactly when using curl in a simple bash script
但是我不确定在简单的 bash 脚本中使用 curl 时在哪里/如何设置它
Question
题
What do I need to change in my curl command to get rid of the error and still be able to use all parameters?
我需要在 curl 命令中更改什么才能消除错误并仍然能够使用所有参数?
UPDATE
更新
Starting a new question, as further investigation has lead me to a better understanding of the problem.
开始一个新问题,因为进一步的调查使我更好地理解了这个问题。
The error has to do with the fact that the parameter datetime= ends up with text in it that needs to be URL encoded.
该错误与参数 datetime= 最终包含需要进行 URL 编码的文本有关。
This was confirmed by replacing the variable with 2017%2F03%2F01%2008%3A50%3A56
通过将变量替换为 2017%2F03%2F01%2008%3A50%3A56 证实了这一点
and it worked.
它奏效了。
So now the problem is, that I can't get --data-urlencode datetime=$datetime to work. It seems this just gets appended to the JSON data or something.
所以现在的问题是,我无法让 --data-urlencode datetime=$datetime 工作。似乎这只是附加到 JSON 数据或其他东西。
采纳答案by DarbyM
This error is being generated by the fact that the datetime= paramater is being passed in with non encoded non URL friendly characters... (eg. space).
这个错误是由 datetime= paramater 以非编码的非 URL 友好字符传入的事实产生的......(例如空格)。
The fix to this would be to find a way to convert the $datetime to a URLEncoded String.
对此的解决方法是找到一种将 $datetime 转换为 URLEncoded 字符串的方法。
eg. convert:
例如。转变:
2017/03/01 08:50:56
TO
到
2017%2F03%2F01%2008%3A50%3A56
See the following discussion for one method to accomplish this.
请参阅以下讨论以了解实现此目的的一种方法。