【问题标题】:Requests module JSONDecodeError请求模块 JSONDecodeError
【发布时间】:2017-08-22 06:37:17
【问题描述】:

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

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 - 信息 - {'Server': 'Apache-Coyote/1.1', 'X-FRAME-OPTIONS': 'SAMEORIGIN, SAMEORIGIN', 'Access-Control-Allow-来源':'*','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 列(字符 0)

有人能帮帮我吗,我得到的状态码是 500

【问题讨论】:

    标签: json python-requests


    【解决方案1】:

    这意味着服务器没有返回任何响应值(没有响应体,所以 json() 无法解码 JSON)。鉴于 500 错误,这可能意味着您的 API 调用错误。不熟悉 API,我不能多说,但我猜你传递的参数是错误的,试试类似:

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

    【讨论】:

      猜你喜欢
      • 2021-03-31
      • 2023-02-04
      • 2019-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多