【问题标题】:Request.post raise jsondecodeerror("expecting value", s, err.value) from noneRequest.post raise jsondecodeerror("expecting value", s, err.value) from none
【发布时间】:2022-06-11 00:03:16
【问题描述】:

当我想使用发布请求 python API (Python 3.9) 发送 JSON 数据时遇到问题。我有一个字符串字典列表,我使用 json.loads() 方法将其转换为 JSON,但它返回错误:

Traceback (most recent call last):
  File "/product/tedh/environment/callminer_py3/lib/python3.9/site-packages/requests/models.py", line 910, in json
    return complexjson.loads(self.text, **kwargs)
  File "/product/tedh/environment/callminer_py3/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/product/tedh/environment/callminer_py3/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/product/tedh/environment/callminer_py3/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我的代码:

consumer_identifiers = bytes(f"{CONSUMER_USERNAME}:{CONSUMER_PASSWORD}", encoding='utf-8')
        headers: requests.structures.CaseInsensitiveDict = CaseInsensitiveDict()
        headers["Basic-Authorization"] = f"Basic {b64encode(consumer_identifiers).decode('ascii')}"
        headers["Authorization"] = f"Bearer {self.__token['access_token']}"
        metadata_body_list = self.__fill_metadata()
        logger.info("Start send metadata to Callminer...")
        logger.info(f"The number of metadata sent is : {len(metadata_body_list)}")
        assert METADATA_INGESTION_URL is not None
        for metadata_body in metadata_body_list:
            json_metadata_body = json.loads(metadata_body)
            try:
                response = requests.post(METADATA_INGESTION_URL, headers=headers, json=json_metadata_body)
                if response.json():
                    if "CorrelationId" in response.json() and "MiningId" in response.json() and \
                            response.json()["CorrelationId"] in metadata_body:
                        logger.debug(f"Response POST request {response.json()}")
                    else:
                        raise RuntimeError(
                            f"Error occurred when sending metadata to Callminer.\n Response: {response.json()}.")
            except requests.exceptions.HTTPError as e:
                raise RuntimeError(f"Error: {str(e)}")
        logger.info("End of sending metadata.")

当我打印 json 时,我发现它是正确的:

{'Metadata': [{'Key': 'udf_text_14', 'Value': 'ACTIF'}, {'Key': 'udf_text_16', 'Value': 'F2K-140 Go 5G'}, {'Key': 'udf_text_17', 'Value': 'None'}, {'Key': 'udf_text_18', 'Value': 'None'}, {'Key': 'udf_text_23', 'Value': '2019-01-15 10:12:21'}, {'Key': 'udf_text_19', 'Value': 'NON'}, {'Key': 'udf_text_21', 'Value': 'OUI'}, {'Key': 'udf_text_20', 'Value': 'NON'}, {'Key': 'udf_text_22', 'Value': 'None'}], 'SourceId': 'SFRALLO', 'Correlationid': '78293941'}

【问题讨论】:

  • JSON 字符串需要双引号,你有单引号。可能重复:stackoverflow.com/questions/4162642/…
  • 感谢您的帮助@Tzane。但是为什么其他的 json 没有错误的通过呢?
  • 我不确定您所说的“其他 jsons”是什么意思,但简单的答案是它们是正确的:P 如果您怀疑这与双引号无关,请尝试将有效和无效的 json 与看看它是否可能是别的东西。

标签: json python-3.x


【解决方案1】:

我终于找到了我的问题的原因,是令牌过期,我的令牌在1小时后过期,我的请求需要更长的时间,因为我有太多的JSON要发送,我在过期之前重新生成它。

【讨论】:

    猜你喜欢
    • 2020-04-19
    • 2019-09-18
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-11
    • 2021-09-29
    • 2019-08-06
    相关资源
    最近更新 更多