请求模块JSONDecodeError

时间:2017-08-22 06:37:18

标签: json python-requests

我有以下代码使用请求模块发布

api_path = r'/DeviceCategory/create'

api_server = (self.base_url + api_path)

logging.info("Triggered API : %s", api_server)

arguments = {"name": "WrongTurn", "vendor": "Cupola", "protocolType": "LWM2M"}

headers = {'content-type': 'application/json;charset=utf-8', 'Accept': '*'}

test_response = requests.post(api_server,headers=headers,cookies=self.jessionid,
                                  timeout=30, json=arguments)

logging.info(test_response.headers)

logging.info(test_response.request)

logging.info(test_response.json())

logging.info(test_response.url)

logging.info(test_response.reason)

我在标题

中得到以下回复
  

2017-08-22 12:03:12,811 - INFO - {'Server':'Apache-Coyote / 1.1','X-FRAME-OPTIONS':'SAMEORIGIN,SAMEORIGIN','Access-Control-Allow- Origin':'*','Access-Control-Allow-Methods':'GET,POST,DELETE,PUT','Access-Control-Allow-Headers':'Content-Type','Content-Type':' text / html; charset = utf-8','Content-Language':'en','Transfer-Encoding':'chunked','Content-Encoding':'gzip','Vary':'Accept-Encoding' ,'日期':'星期二,2017年8月22日06:33:12 GMT','连接':'关闭'}

JSON解码错误

  

从None中引发JSONDecodeError(“Expecting value”,s,err.value)   json.decoder.JSONDecodeError:期望值:第1行第1列(char 0)

有些人可以帮帮我,我得到的状态代码是500

1 个答案:

答案 0 :(得分:0)

这意味着服务器没有返回任何响应值(没有响应主体,因此json()无法解码JSON)。考虑到500错误,这可能意味着您的API调用不正确。不熟悉API,我无法多说,但我的猜测是您传递的参数有误,请尝试以下操作:

test_response = requests.post(api_server,headers=headers,cookies=self.jessionid,
                                  timeout=30, data=arguments)