如何在 HTTP GET 请求中发送原始数据?

发布于 2024-10-08 07:30:07 字数 596 浏览 2 评论 0原文

http://alx3apps.appspot.com/jsonrpc_example/ 的示例中,当我单击提交按钮时,我注意到(通过使用 Firebug)我的浏览器提交了源代码:

{"params":["Hello ","Python!"],"method":"concat","id":1}

没有发布参数(例如json=[上面的编码字符串]),而只是发布具有上述值的原始字符串。

是否有一种广泛接受的方法来通过 GET 请求复制此内容,或者我是否需要对相同的字符串进行 urlencode 并将其包含为 http://www.example.com/?json=%7b%22params%22 %3a%5b%22Hello+%22%2c%22Python!%22%5d%2c%22method%22%3a%22concat%22%2c%22id%22%3a1%7d?我知道一些较旧的浏览器无法处理超过 250 个字符的 URI,但我对此表示同意。

In the example at http://alx3apps.appspot.com/jsonrpc_example/ when I click the submit button, I notice (by using Firebug) that my browser submits the source:

{"params":["Hello ","Python!"],"method":"concat","id":1}

It's not posting a parameter (eg. json=[encoded string from above]), but rather just posting a raw string with the above value.

Is there an widely accepted way to replicated this via a GET request, or do I need to just urlencode the same string and include it as http://www.example.com/?json=%7b%22params%22%3a%5b%22Hello+%22%2c%22Python!%22%5d%2c%22method%22%3a%22concat%22%2c%22id%22%3a1%7d? I understand that some older browsers cannot handle a URI of more than 250 characters, but I'm OK with that.

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

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

发布评论

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

评论(2

來不及說愛妳 2024-10-15 07:30:07

除了标头之外,GET 请求通常不会以任何其他方式传输数据,因此如果您希望使用 GET,则应该传递 URL 中编码的字符串。

POST http://alx3apps.appspot.com/jsonrpc_example/json_service/ HTTP/1.1
Host: alx3apps.appspot.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Content-Type: application/json-rpc; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://alx3apps.appspot.com/jsonrpc_example/
Content-Length: 55
Pragma: no-cache
Cache-Control: no-cache

{"params":["Howdy","Python!"],"method":"concat","id":1}

在普通表单中,标题 Content-Type: application/x-www-form-urlencoded 让服务器知道期望采用 key=val 格式的格式,而您链接的页面发送 Content -类型:application/json-rpc;字符集=UTF-8。在标题(以空行结束)之后是指定格式的数据。

A GET request doesn't usually transmit data in any other way besides headers, so you should pass the string encoded in the URL if you wish to use GET.

POST http://alx3apps.appspot.com/jsonrpc_example/json_service/ HTTP/1.1
Host: alx3apps.appspot.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Content-Type: application/json-rpc; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://alx3apps.appspot.com/jsonrpc_example/
Content-Length: 55
Pragma: no-cache
Cache-Control: no-cache

{"params":["Howdy","Python!"],"method":"concat","id":1}

In a normal form post the header Content-Type: application/x-www-form-urlencoded lets the server know to expect the format in key=val format whereas the page you linked sends Content-Type: application/json-rpc; charset=UTF-8. After the headers (which are terminated with the blank line) the data follows in the specified format.

岁月静好 2024-10-15 07:30:07

您是对的,只有 POST 才能与 URI 分开提交数据。因此,如果必须使用 GET,则将其 urlencode 编码到查询字符串中是唯一的方法。 (好吧,我想您可以尝试设置自定义请求标头或使用 cookie,但唯一“广泛接受”的方法是使用查询字符串。)

You are correct that only POST submits data separately from the URI. So urlencoding it into the querystring is the only way to go, if you must use GET. (Well, I suppose you could try setting custom request headers or using cookies, but the only "widely accepted" way is to use the querystring.)

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