使用 cURL 编码的 POST url

发布于 2024-12-02 08:13:40 字数 322 浏览 3 评论 0原文

当我必须使用 CURL 发送 POST 请求时,在 POST 字段中,如果它们包含带空格的字符串,我是否必须对字符串进行 url_encode,或者该函数会为我执行此操作?

什么是正确的?

CURLOPT_POSTFIELDS  => 'field=this%20is%20good'

或者

CURLOPT_POSTFIELDS  => 'field=this is good'

我问这个是因为我不希望它对字符串进行两次编码从而发送错误的数据。

谢谢

when I have to send a POST request with CURL, in the POST fields if they contain strings with spaces do I have to url_encode the strings or the function will do it for me?

what's correct?

CURLOPT_POSTFIELDS  => 'field=this%20is%20good'

or this

CURLOPT_POSTFIELDS  => 'field=this is good'

I'm asking because I don't want it to encode the string twice and thus send incorrect data.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒路情人 2024-12-09 08:13:40

你没有指定,但我想你使用的是 PHP,对吗?

它必须根据 RFC 1738 进行 urlencoded。
您可以使用函数http_build_query()。例如,我使用:

$array = array('field' => 'this is good');
http_build_query($array, '', '&');

您也可以简单地传递 $array 作为选项(如 CURLOPT_POSTFIELDS => $array),但这将创建一个 multipart/form-data 请求,而不是“正常” “应用程序/x-www-form-urlencoded。

PS:实际上,你的都不是正确的:)你应该根据 RFC 1738 用 + 编码空格,而不是用 %20 编码。

You did not specify that, but I suppose you're using PHP, right?

It must be urlencoded according to RFC 1738.
You can use the function http_build_query(). I use, for example:

$array = array('field' => 'this is good');
http_build_query($array, '', '&');

You could also simply pass $array as the option (like CURLOPT_POSTFIELDS => $array), but this will create a multipart/form-data request, instead of the "normal" application/x-www-form-urlencoded.

PS: None of yours are correct, actually :) You should encode spaces with +, as per the RFC 1738, and not with %20.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文