Python中的JSON POST调用不起作用

Python中的JSON POST调用不起作用,python,json,rest,post,Python,Json,Rest,Post,我是python和PostMessage的新手 我试图使用JSON POST消息调用API,并期望得到JSON响应,但我的初始代码无法根据需要进行调用 在chrome浏览器的帖子扩展中使用URL、标题和帖子数据可以正常工作。 #!/usr/bin/python import requests import json url = 'http://xxxxx:111/batches' postdata = { "active": "true", "size": "2",

我是python和PostMessage的新手

我试图使用JSON POST消息调用API,并期望得到JSON响应,但我的初始代码无法根据需要进行调用

在chrome浏览器的帖子扩展中使用URL、标题和帖子数据可以正常工作。

#!/usr/bin/python

import requests
import json

url = 'http://xxxxx:111/batches'
postdata = {
    "active": "true",
    "size": "2",
    "ctr": {
        "user": "Admin",
        "id": "1234"}}

#headers = {'content-type': 'application/json',
                         #'Authorization': 'Basic xyz879jjkhhnm',
                         #'Accept-Encoding': '0'}
headers = {'Authorization': 'Basic xyz879jjkhhnm', 'Accept-Encoding': '0'}
print headers

post_call = requests.post(url, headers=headers, data=json.dumps(postdata))
print post_call, "POST call"

print post_call.text, "TEXT"
print post_call.content, "CONTENT"

post_call.status_code, "STATUS CODE"


Error:
{'Accept-Encoding': '0', 'Authorization': 'Basic xyz879jjkhhnm'}
<Response [500]> POST call
[{"code":"server_error","description":"com.sun.jersey.api.MessageException: A message body reader for Java class com.hide.cpn.rest.v1.entity.CouponCodeBatchResourceEntity, 
and Java type class com.hide.cpn.rest.v1.entity.CouponCodeBatchResourceEntity, and MIME media type application/octet-stream was not found.
\nThe registered message body readers compatible with the MIME media type are:\napplication/octet-stream ->\n  
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider\n  com.sun.jersey.core.impl.provider.entity.FileProvider\n  
com.sun.jersey.core.impl.provider.entity.InputStreamProvider\n  com.sun.jersey.core.impl.provider.entity.DataSourceProvider\n  
com.sun.jersey.core.impl.provider.entity.RenderedImageProvider\n*/* ->\n  com.sun.jersey.core.impl.provider.entity.FormProvider\n  
com.sun.jersey.core.impl.provider.entity.StringProvider\n  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider\n  
com.sun.jersey.core.impl.provider.entity.FileProvider\n  com.sun.jersey.core.impl.provider.entity.InputStreamProvider\n  
com.sun.jersey.core.impl.provider.entity.DataSourceProvider\n  com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General\n  
com.sun.jersey.core.impl.provider.entity.ReaderProvider\n  com.sun.jersey.core.impl.provider.entity.DocumentProvider\n  
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader\n  com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader\n  
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader\n  com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General\n  
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General\n  com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General\n  
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General\n  com.sun.jersey.core.impl.provider.entity.EntityHolderReader\n  
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General\n  com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General\n  
com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy\n","errorValues":null}] TEXT


**Edit:1 (changing Headers seems to get ridoff Java error now ,but getting below error now)**

headers = {'Authorization': 'Basic xyz879jjkhhnm', 'Accept-Encoding': '0', 'content-type': 'application/json'}

New error:
{'content-type': 'application/json', 'Accept-Encoding': '0', 'Authorization': 'Basic xyz879jjkhhnm'}
<Response [400]> POST call
[{"code":"invalid_attribute_of_request","description":"Attribute value type is not Integer. actual value = (\"2\")","errorValues":null,"field":"size"}] TEXT
[{"code":"invalid_attribute_of_request","description":"Attribute value type is not Integer. actual value = (\"2\")","errorValues":null,"field":"size"}] CONTENT
#/usr/bin/python
导入请求
导入json
url='1〕http://xxxxx:111/batches'
postdata={
“活动”:“真实”,
“大小”:“2”,
“ctr”:{
“用户”:“管理员”,
“id”:“1234”}
#headers={'content-type':'application/json',
#“授权”:“基本xyz879jjkhhnm”,
#“接受编码”:“0”}
headers={'Authorization':'Basic xyz879jjkhhnm','Accept Encoding':'0'}
打印标题
post_call=requests.post(url,headers=headers,data=json.dumps(postdata))
打印post_呼叫,“post呼叫”
打印post_call.text,“text”
打印post_call.content,“content”
post_call.status_代码,“状态代码”
错误:
{'Accept-Encoding':'0','Authorization':'Basic xyz879jjkhhnm'}
邮电
[{“code”:“server_error”,“description”:“com.sun.jersey.api.MessageException:Java类com.hide.cpn.rest.v1.entity.CouponCodeBatchResourceEntity的消息体读取器,
和Java类型类com.hide.cpn.rest.v1.entity.coupOnDebatchResourceEntity,未找到MIME媒体类型应用程序/八位字节流。
\与MIME媒体类型兼容的已注册邮件正文读取器为:\n应用程序/octet流->\n
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider\n com.sun.jersey.core.impl.provider.entity.FileProvider\n
com.sun.jersey.core.impl.provider.entity.InputStreamProvider\n com.sun.jersey.core.impl.provider.entity.DataSourceProvider\n
com.sun.jersey.core.impl.provider.entity.renderImageProvider\n*/*->\n com.sun.jersey.core.impl.provider.entity.FormProvider\n
com.sun.jersey.core.impl.provider.entity.StringProvider\n com.sun.jersey.core.impl.provider.entity.ByteArrayProvider\n
com.sun.jersey.core.impl.provider.entity.FileProvider\n com.sun.jersey.core.impl.provider.entity.InputStreamProvider\n
com.sun.jersey.core.impl.provider.entity.DataSourceProvider\n com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General\n
com.sun.jersey.core.impl.provider.entity.ReaderProvider\n com.sun.jersey.core.impl.provider.entity.DocumentProvider\n
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader\n com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader\n
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader\n com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General\n
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General\n com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General\n
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General\n com.sun.jersey.core.impl.provider.entity.EntityHolderReader\n
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General\n com.sun.jersey.json.impl.provider.entity.jsonListerElementProvider$General\n
com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy\n,“errorValues”:null}]TEXT
**编辑:1(更改标题现在似乎得到了ridoff Java错误,但现在得到了下面的错误)**
headers={'Authorization':'Basic xyz879jjkhhnm','Accept Encoding':'0','content type':'application/json'}
新错误:
{'content-type':'application/json','Accept Encoding':'0','Authorization':'Basic xyz879jjkhhnm'}
邮电
[{“代码”:“请求的属性无效”,“说明”:“属性值类型不是整数。实际值=(\“2\”),“错误值”:null,“字段”:“大小”}]TEXT
[{“代码”:“请求的\u属性\u无效”,“说明”:“属性值类型不是整数。实际值=(\“2\”),“错误值”:null,“字段”:“大小”}]CONTENT

好的,根据(编辑1)中最新的更改标题更改,我必须删除post数据“大小”中的双引号,这解决了我的错误

headers = {'Authorization': 'Basic xyz879jjkhhnm', 'Accept-Encoding': '0', 'content-type': 'application/json'}


    postdata = {
        "active": "true",
        "size": "2", --> Changed to "size": 2,
        "ctr": {
            "user": "Admin",
            "id": "1234"}}

您将得到一个500错误,这表明在尝试执行此命令时,您的代码中出现了一些错误。错误响应表明它将输入识别为特定的Java类类型,但没有与之兼容的消息体读取器。我会遵循这个思路。在您最初的版本中,您没有这里的内容类型标题。对,这就是我必须进行的更改,以避免java错误,然后将大小更改为整数