关于 curl 命令提交带有空格的内容。

2020 年 7 月 15 日
 nightwiz

请教一下,我用 curl -d 提交内容到企业微信机器人。内容有引用变量,变量值里面带有空格。使用 curl 提交会报错。请问这个有什么好的解决方法。

3371 次点击
所在节点    Linux
12 条回复
msg7086
2020 年 7 月 15 日
只看你说的这些,我只能回答:根据报错信息对应修改了。
如果多贴点信息的话可能更好地帮助你。
hush3
2020 年 7 月 15 日
%20?
momocraft
2020 年 7 月 15 日
学习 shell 的引号 escape?
nightwiz
2020 年 7 月 15 日
@hush3 是的可以用%20 替换空格部分,但是因为是通过变量获取到的内容。。这个要怎么替换呢
ysc3839
2020 年 7 月 15 日
@nightwiz 是什么语言,找对应的 urlencode 方法。
ai277014717
2020 年 7 月 15 日
加引号
lxk11153
2020 年 7 月 15 日
> man curl
--data-urlencode <data>
(HTTP) This posts data, similar to the other -d, --data options with the exception that this performs URL-encoding.
megatron7
2020 年 7 月 15 日
处理过类似的
"content": "'"$1"'"
用引号包裹内容
nightwiz
2020 年 7 月 15 日
@megatron7 谢谢,你的方法可以。
lxk11153
2020 年 7 月 15 日
> curl -s --data-urlencode "a=& &" "b=$your" https://httpbin.org/post
{
"form": {
"a": "& &"
}
"headers": {
"Content-Type": "application/x-www-form-urlencoded"
}
}

你可以换到 `-d` 看看区别,(如果你的内容包含&等特殊字符的话
lxk11153
2020 年 7 月 15 日
fix 少打了一个
curl -s --data-urlencode "a=& &" --data-urlencode "b=$your" url
megatron7
2020 年 7 月 16 日
@nightwiz
最近又试了另一个方式 @file_name 也是可以的

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/690171

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX