有什么方法可以在 curl 命令中对 url 进行编码?

Aas*_* P. 133 url curl

我有一些 url,它的查询参数中有空格。我想在 curl 中使用它,例如

curl -G "http://localhost:30001/data?zip=47401&utc_begin=2013-8-1 00:00:00&utc_end=2013-8-2 00:00:00&country_code=USA"
Run Code Online (Sandbox Code Playgroud)

这给出了

Malformed Request-Line
Run Code Online (Sandbox Code Playgroud)

根据我的理解,o/p 是由于查询参数中存在空间。

在将 url 提供给 curl 命令之前,是否可以自动对 url 进行编码?

小智 219

curl内部支持 url 编码--data-urlencode

$ curl -G -v "http://localhost:30001/data" --data-urlencode "msg=hello world" --data-urlencode "msg2=hello world2"
Run Code Online (Sandbox Code Playgroud)

-G 还需要将数据附加到 URL。

跟踪标头

> GET /data?msg=hello%20world&msg2=hello%20world2 HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu)
> Host: localhost
> Accept: */*
Run Code Online (Sandbox Code Playgroud)

  • @GaneshSatpute:使用多个`--data-urlencode` 参数,每个键值对一个。 (12认同)
  • 是否可以对 POST 请求执行查询参数 url 编码**并**提供有效负载? (2认同)

Dra*_*oan 8

 curl -G "$( echo "$URL" | sed 's/ /%20/g' )"
Run Code Online (Sandbox Code Playgroud)

$URL您要进行翻译的网址在哪里。

您可以在 URL 中使用不止一种类型的翻译(编码),因此您可能想要执行以下操作:

curl -G "$(perl -MURI::Escape -e 'print uri_escape shift, , q{^A-Za-z0-9\-._~/:}' -- "$URL")"
Run Code Online (Sandbox Code Playgroud)

反而。

  • `sed 's/ /%20/g'` 如果你有多个空格需要翻译... (2认同)