将包含 json 的原始正文提交给 REST api

发布于 2024-12-21 01:15:25 字数 290 浏览 1 评论 0原文

我正在编写一个休息服务,它接受带有 http post 的 json 文档。 我可以使用“Chrome Rest Console”插件将 JSON 文档放入原始正文字段中来提交数据。

我正在尝试使用 urllib2 来实现相同的目标。 使用 urllib2 我无法发布数据,除非我对一个或多个与 Web 服务的 cgi 表单字段相对应的键/值对进行 urlencode。 但是,我不想使用 cgi 表单字段,因为我的休息服务没有任何字段, 我只想提交原始 json 文档...

我该怎么做?

谢谢,

I'm writing a rest service which accepts json documents with http post.
I can submit my data using my "Chrome Rest Console" plugin by putting a JSON document into the raw body field.

I'm trying to achieve the same using urllib2.
Using urllib2 i'm not able to post data unless I urlencode one or more key/value pairs which correspond to the cgi form fields of the web service.
However, I don't want to make use of cgi form fields, as my rest service doesn't have any,
I just want to submit a raw json document ...

How can I do that?

Thanks,

J

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

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

发布评论

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

评论(1

不乱于心 2024-12-28 01:15:25

我不会将问题标记为完全重复,因为我不确定这就是您的意思。然而,从这个其他问题

import json
import urllib2
data = json.dumps([1, 2, 3])
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
response = f.read()
f.close()

I'm not flagging the question as exact duplicate as I'm not sure this is what you meant. However from this other question:

import json
import urllib2
data = json.dumps([1, 2, 3])
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
response = f.read()
f.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文