【问题标题】:Getting error curl: (3) Port number ended with ' ' while executing curl POST from bash script获取错误 curl:(3)从 bash 脚本执行 curl POST 时,端口号以“”结尾
【发布时间】:2020-01-26 07:57:34
【问题描述】:

我正在尝试从 bash 脚本执行 curl,并在运行 POST curl 命令时返回 curl: (3) 端口号以 ' ' 错误结尾。

例如script.sh 包含以下 curl

curl -k -X -H "content-type: application/json" POST -d 
  '{\"test_field\": \"test.com\",
     \"head\": \"-----BEGIN REQUEST-----\n
MIICvzCCAacCAQAwejgVPmU0J/BUbIuyOC5KXMYkc2KqggRBLpLdKs8ODo5cVKzVa7UOmNGRdGncM1WC0HHnn8E47fYnjmn72WQ94L1kBqJuM+olYbOtyop84lmYo0LTm0DH/w4DBlzUdp4 Z8EYFuRtv1En2TkJ6YxI86z7Bwc/I20HPZPQYEQ4v=\n
-----END CERTIFICATE REQUEST-----\"
  }' https://mytestserver/v1/testfield

结果:

curl: (3) Port number ended with ' '
curl: (6) Could not resolve host: POST
{"errors":[]}

如果我从邮递员执行相同的正文内容,它会成功执行。

内容

-----BEGIN REQUEST-----
MIICvzCCAacCAQAwejgVPmU0J/BUbIuyOC5KXMYkc2KqggRBLpLdKs8ODo5cVKzVa7UOmNGRdGncM1WC0HHnn8E47fYnjmn72WQ94L1kBqJuM+olYbOtyop84lmYo0LTm0DH/w4DBlzUdp4 Z8EYFuRtv1En2TkJ6YxI86z7Bwc/I20HPZPQYEQ4v=
-----END REQUEST-----

有空格和 / 特殊字符。我试图逃脱它,但没有成功。

【问题讨论】:

  • 不确定您的 curl 上下文,但在常规 shell 脚本上下文中,'single-quoted data "dbl-quoted data" should be sufficient'(无需转义 dbl 引号)。祝你好运。

标签: bash curl sh


【解决方案1】:

错误消息表明“POST”参数被误认为是主机名。很可能,因为 Curl 期望 '-X' 后跟 (http) 命令。

根据'curl'手册页:通常你不需要这个选项。各种 GET、HEAD、POST 和 PUT 请求都是通过使用专用命令行选项来调用的

考虑其中一种选择:

curl -k -X POST -H 'content-type: ...' -d '{ ...}' https://mytestserver/...

或者,让 curl 发挥作用:推荐

curl -k -H 'content-type: ...' -d '{ ...}' https://mytestserver/...

【讨论】:

    猜你喜欢
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 2018-08-29
    • 2021-07-19
    相关资源
    最近更新 更多