如何使用curl以UTF-8编码发布表单数据?

ash*_*aka 18 post webserver encoding curl http

我想在终端提示符下POST使用(发送)一些表单数据到网络服务器cURL.

这是我到目前为止所得到的:

curl --data-ascii "content=derinhält&date=asdf" http://myserverurl.com/api/v1/somemethod
Run Code Online (Sandbox Code Playgroud)

问题是变形金刚("äöü")被"?"取代 当我在服务器上收到邮件请求时.

我想我需要为POST请求使用UTF-8编码.

有谁知道我怎么能做到这一点?

Kir*_*nov 29

您可以在POST请求中使用UTF-8,您只需在请求中指定字符集即可.

您应该使用此请求:

curl -X POST -H "Content-Type: application/x-www-form-urlencoded; charset=utf-8" --data-ascii "content=derinhält&date=asdf" http://myserverurl.com/api/v1/somemethod
Run Code Online (Sandbox Code Playgroud)

  • 上面答案中给出的内容类型是错误的.正确的是`-H"Content-Type:application/x-www-form-urlencoded; charset = utf-8"`. (8认同)
  • 对我来说,使用非 URL 编码内容指定“urlencoded”内容类型似乎是错误的。而是使用 `--data-urlencode` 并且如果文本不是 UTF-8 编码,您可以使用 `"content=$(echo -n "derinhält" | iconv -f ISO-8859-1 -t UTF-8 )"` (3认同)