代码之家  ›  专栏  ›  技术社区  ›  Philip Mutua

“error”:“json分析错误-应为值:第1行第1列(字符0)”“如何解决此问题”

  •  1
  • Philip Mutua  · 技术社区  · 6 年前

    我正在编写一个脚本来从不同的web服务发出请求。我在从下面的json数据发布数据时遇到问题。当我运行 patient_create_bill() 函数i从response获取下面的日志。

    RESPONSE IS!!!!!
    {'Error': 'JSON parse error - Expecting value: line 1 column 1 (char 0)'}
    DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 0.0.0.0:9000
    DEBUG:urllib3.connectionpool:http://0.0.0.0:9000 "POST /api/bill/patient/bills/ HTTP/1.1" 400 72
    Creating Patient Bill .....................................
    

    我试着做一个 POST 在邮递员那里 201 响应意味着有效载荷是好的没有什么问题。 enter image description here

    这是我的 岗位 有效载荷。
    我有一个单独的文件叫 mocks.py 包含 有 PATIENT_BILL_CREATE_PAYLOAD

    PATIENT_BILL_CREATE_PAYLOAD = {
        "bill_items": [{
            "item": "Syringes",
            "qty": 2,
            "description": "Medicorp syringes"
        }],
        "bill_services": [{
            "service": "Diagnosis",
            "service_type": "1",
            "duration": 5,
            "description": "diagnosis"
        }],
        "client": "Sandra Hernandez"
    }
    

    这是功能
    我已经进口了 病人创建有效载荷 在这个函数中使用它。

    def patient_create_bill(headers):
        """This function uses login creds provided and returns token plus logged in user data."""
        url = "http://0.0.0.0:9000/api/bill/patient/bills/"
        data = PATIENT_BILL_CREATE_PAYLOAD
        res = requests.post(url, data=data, headers=headers)
        res_data = res.json()
        print("Creating Patient Bill .....................................\n")
        return res_data
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   Rezvanov Maxim    6 年前

    此日志告诉您在http响应(http代码400)中未收到正文: DEBUG:urllib3.connectionpool:http://0.0.0.0:9000 "POST /api/bill/patient/bills/ HTTP/1.1" 400 72

    python试图解析emtry字符串。 您可以运行此:

    import json
    json.loads('')
    

    这段代码将引发:

    json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
    

    我认为,你应该检查你的url端点来调用。

        2
  •  0
  •   Philip Mutua    6 年前

    我的错误是因为在请求中使用数据而不是json,并且还指定了头 Content-Type 作为'application/json:-)。

    推荐文章