V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
nightwiz
V2EX  ›  Linux

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

  •  
  •   nightwiz · 2020-07-15 11:11:26 +08:00 · 2672 次点击
    这是一个创建于 1353 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    12 条回复    2020-07-16 19:10:48 +08:00
    msg7086
        1
    msg7086  
       2020-07-15 11:16:15 +08:00
    只看你说的这些,我只能回答:根据报错信息对应修改了。
    如果多贴点信息的话可能更好地帮助你。
    hush3
        2
    hush3  
       2020-07-15 11:21:28 +08:00
    %20?
    momocraft
        3
    momocraft  
       2020-07-15 11:27:23 +08:00
    学习 shell 的引号 escape?
    nightwiz
        4
    nightwiz  
    OP
       2020-07-15 11:33:11 +08:00
    @hush3 是的可以用%20 替换空格部分,但是因为是通过变量获取到的内容。。这个要怎么替换呢
    ysc3839
        5
    ysc3839  
       2020-07-15 12:19:57 +08:00 via Android
    @nightwiz 是什么语言,找对应的 urlencode 方法。
    ai277014717
        6
    ai277014717  
       2020-07-15 12:23:36 +08:00
    加引号
    lxk11153
        7
    lxk11153  
       2020-07-15 13:39:35 +08:00   ❤️ 1
    > 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
        8
    megatron7  
       2020-07-15 14:56:02 +08:00
    处理过类似的
    "content": "'"$1"'"
    用引号包裹内容
    nightwiz
        9
    nightwiz  
    OP
       2020-07-15 15:25:38 +08:00
    @megatron7 谢谢,你的方法可以。
    lxk11153
        10
    lxk11153  
       2020-07-15 15:49:31 +08:00
    > curl -s --data-urlencode "a=& &" "b=$your" https://httpbin.org/post
    {
    "form": {
    "a": "& &"
    }
    "headers": {
    "Content-Type": "application/x-www-form-urlencoded"
    }
    }

    你可以换到 `-d` 看看区别,(如果你的内容包含&等特殊字符的话
    lxk11153
        11
    lxk11153  
       2020-07-15 15:52:16 +08:00
    fix 少打了一个
    curl -s --data-urlencode "a=& &" --data-urlencode "b=$your" url
    megatron7
        12
    megatron7  
       2020-07-16 19:10:48 +08:00
    @nightwiz
    最近又试了另一个方式 @file_name 也是可以的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5330 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 08:27 · PVG 16:27 · LAX 01:27 · JFK 04:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.