【问题标题】:JSON POST call in Python not workingPython中的JSON POST调用不起作用
【发布时间】:2015-09-03 17:43:42
【问题描述】:

我是 python 和 POST 消息的新手。

我正在尝试使用 JSON POST 消息调用 API 并期待 JSON 响应,但我的初始代码无法按要求进行调用。

在 chrome 浏览器 POST 扩展中使用 URL、Headers 和 Postdata 可以正常工作。

#!/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

【问题讨论】:

  • 您收到 500 错误,这表明在尝试执行时您的代码有问题。错误响应表明它将输入识别为特定的 Java 类类型,但没有与之兼容的消息正文阅读器。我会关注这个帖子。

标签: python json rest post


【解决方案1】:

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

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"}}

【讨论】:

  • 在您的原始文件中,您没有此处的内容类型标题。
  • 对,这就是我必须做的改变以避免java错误,然后将大小更改为整数
猜你喜欢
  • 2012-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-04
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多