使用 Python-Request 发布 REST 帖子 [英] REST post using Python-Request

查看:35
本文介绍了使用 Python-Request 发布 REST 帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个简单的代码不向我的服务发送数据:

导入请求导入json数据 = {数据":24.3"}data_json = json.dumps(data)response = requests.post(url, data=data_json)打印 response.text

我的服务是使用 WCF 开发的,如下所示:

 [操作契约][WebInvoke(Method = "POST", UriTemplate = "/test", ResponseFormat =WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json)]字符串测试(字符串数据);

注意:如果删除输入参数 data 一切正常,可能是什么问题.

解决方案

需要设置内容类型标题:

data = {"data" : "24.3"}data_json = json.dumps(data)标头 = {'内容类型':'应用程序/json'}response = requests.post(url, data=data_json, headers=headers)

如果我将 url 设置为 http://httpbin.org/post,该服务器会向我回显发布的内容:

<预><代码>>>>导入json>>>进口请求>>>导入打印>>>url = 'http://httpbin.org/post'>>>数据 = {数据":24.3"}>>>data_json = json.dumps(data)>>>标头 = {'内容类型':'应用程序/json'}>>>response = requests.post(url, data=data_json, headers=headers)>>>pprint.pprint(response.json()){u'args':{},u'data': u'{"data": "24.3"}',你'文件':{},你'形式':{},u'headers': {u'Accept': u'*/*',u'Accept-Encoding': u'gzip, deflate, compress',u'Connection': u'keep-alive',u'内容长度':u'16',u'Content-Type': u'application/json',u'Host': u'httpbin.org',u'用户代理':u'python-requests/1.0.3 CPython/2.6.8 Darwin/11.4.2'},u'json': {u'data': u'24.3'},u'origin': u'109.247.40.35',u'url': u'http://httpbin.org/post'}>>>pprint.pprint(response.json()['json']){u'数据':u'24.3'}

如果您使用的是 requests 2.4.2 或更新版本,您可以将 JSON 编码留给库;它也会自动为您设置正确的 Content-Type 标头.将要作为 JSON 发送的数据传入 json 关键字参数:

data = {"data" : "24.3"}response = requests.post(url, json=data)

Why doesn't this simple code POST data to my service:

import requests
import json

data = {"data" : "24.3"}
data_json = json.dumps(data)
response = requests.post(url, data=data_json)
print response.text

And my service is developed using WCF like this :

  [OperationContract]
  [WebInvoke(Method = "POST", UriTemplate = "/test", ResponseFormat =    
      WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json)]
  string test(string data );

Note: If is remove the input parameter data everything works fine, what may be the issue.

解决方案

You need to set the content type header:

data = {"data" : "24.3"}
data_json = json.dumps(data)
headers = {'Content-type': 'application/json'}

response = requests.post(url, data=data_json, headers=headers)

If I set url to http://httpbin.org/post, that server echos back to me what was posted:

>>> import json
>>> import requests
>>> import pprint
>>> url = 'http://httpbin.org/post'
>>> data = {"data" : "24.3"}
>>> data_json = json.dumps(data)
>>> headers = {'Content-type': 'application/json'}
>>> response = requests.post(url, data=data_json, headers=headers)
>>> pprint.pprint(response.json())
{u'args': {},
 u'data': u'{"data": "24.3"}',
 u'files': {},
 u'form': {},
 u'headers': {u'Accept': u'*/*',
              u'Accept-Encoding': u'gzip, deflate, compress',
              u'Connection': u'keep-alive',
              u'Content-Length': u'16',
              u'Content-Type': u'application/json',
              u'Host': u'httpbin.org',
              u'User-Agent': u'python-requests/1.0.3 CPython/2.6.8 Darwin/11.4.2'},
 u'json': {u'data': u'24.3'},
 u'origin': u'109.247.40.35',
 u'url': u'http://httpbin.org/post'}
>>> pprint.pprint(response.json()['json'])
{u'data': u'24.3'}

If you are using requests version 2.4.2 or newer, you can leave the JSON encoding to the library; it'll automatically set the correct Content-Type header for you too. Pass in the data to be sent as JSON into the json keyword argument:

data = {"data" : "24.3"}
response = requests.post(url, json=data)

这篇关于使用 Python-Request 发布 REST 帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆