bash 使用 curl POST 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20922887/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:09:50  来源:igfitidea点击:

POST data with curl

bashcurl

提问by John Williams

Bash n00b here.. I'm posting a file b64 encoded like this using curl:

在这里 Bash n00b .. 我正在使用 curl 发布一个像这样编码的 b64 文件:

$ cat file.txt | openssl base64 | curl --data @- myhost.com/api

Works good. I split the key/value on the server side, the entire message goes into the key, but that's ok I parse it out and convert to ascii etc.. on the server.

效果很好。我在服务器端拆分键/值,整个消息进入键,但没关系我解析它并在服务器上转换为 ascii 等。

How can I append other key/values to the post? Something like..

如何将其他键/值附加到帖子中?就像是..

$ cat file.txt | openssl base64 | PREPEND "key=value1&key2&value2&btext=" | curl --data @- myhost.com/api

回答by Sean

You can create all of the input to curl in a subshell, like so:

您可以在子外壳中创建所有输入以 curl ,如下所示:

(echo -n "key=value1&key2=value2&btext="; openssl base64 < file.txt) | curl --data @- myhost.com/api

This will execute echoand opensslafter each other and pass concatenated output to curl.

这将在彼此之后执行echo并将openssl连接的输出传递给curl.

回答by ride

Instead of @-, you can use -F to post both your prepended keys and the base64 text.

除了@-,您可以使用 -F 来发布您的前置键和 base64 文本。

curl -F key=value1 -F key2=value2 -F btext=$(openssl base64 < file.txt | tr -d "\n") myhost.com/api

In my testing, base64 won't be sent correctly unless you remove linebreaks, and neither -F nor @- will work for particularly large files (>~50kb?).

在我的测试中,除非您删除换行符,否则不会正确发送 base64,并且 -F 和 @- 都不适用于特别大的文件(>~50kb?)。