【问题标题】:Post request in python giving me a "could not parse request body" error? Check JSON? But it is in JSON在 python 中发布请求给我一个“无法解析请求正文”错误?检查 JSON?但它是 JSON
【发布时间】:2021-04-12 02:54:50
【问题描述】:

我收到这条消息{"error":"Could not parse request body. Please check JSON format"}

以下请求

import requests
import json

link = "https://api.3dsintegrator.com/v2/authenticate/browser"

data = {
    "pan":"",
    "amount":"89.95",
    "month":"01",
    "year":"27",
    "shipping": {
            "line1":"",
            "line2":"",
            "postCode":"78541",
            "city":"",
            "state":"",
            "country":""
            },
    "billing": {"line1":"","line2":"","city":"Edinburg","state":"","country":"840"},
    "cardHolderName":"",
    "messageCategory":"01",
    "browser": {"browserAcceptHeader":"application/json","browserJavaEnabled":"false","browserJavascriptEnabled":"true","browserLanguage":"en-US","browserColorDepth":"24","browserScreenWidth":"3200","browserScreenHeight":"1333","browserTZ":"420","browserUserAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:87.0) Gecko/20100101 Firefox/87.0"},
    "challengeIndicator":"02",
    "challengeWindowSize": "01"
}
headers = {
    "Accept": "*/*",
    "Accept-Language": "en-US,en;q=0.5",
    "Accept-Encoding": "gzip, deflate, br",
    "Content-Type": "application/json",
    "X-3DS-API-KEY": "",
    "X-3DS-SDK-VERSION": "",
    "Authorization": "Bearer ",
    "Content-Length": "731",
    "Origin": "https://winbigbonus.com",
    "DNT": "1",
    "Connection": "keep-alive",
    "Referer": "https://winbigbonus.com/",
    "TE": "Trailers",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:87.0) Gecko/20100101 Firefox/87.0"
}

response = requests.post(link, headers=headers, data=data).text
print(response)

出于隐私原因,我删除了大部分值。但我相信我的数据对象已经是 json 格式,所以不知道为什么它会导致正文错误并说它无法解析它?

【问题讨论】:

  • 我将您的数据包装在 """{ all data same as your} """ 之间,现在将其转换为服务器方法所需的方式。 print("数据类型",type(data)) json_str=json.dumps(data) print("json_str类型",type(json_str)) json_back=json.loads(data) print("json_back类型", type(json_back)) json_back["amount"] --->

标签: python json https python-requests http-post


【解决方案1】:

您的数据值看起来像字典,而不是 JSON 字符串。试试:

response = requests.post(link, headers=headers, data=json.dumps(data)).text

【讨论】:

  • 它说 AttributeError: module 'json' has no attribute 'parse'
  • 我的错;应该使用转储。我刚刚回答了一个关于 javascript 的问题,有时会将两者混淆。
  • hmm 似乎出现了同样的错误。那么可能不是导致错误的数据对象?
  • 在帖子里面,你不是在为你输入的东西做一个 json.loads(),然后为你输出的东西做一个 json.dumps() 吗?
  • 我也尝试将 json.dumps 添加到标题中,但出现属性错误:“str”对象没有属性项
【解决方案2】:

对于 3DS 2.1.0,您需要将金额值作为浮点数而不是字符串传递

"amount":89.95

【讨论】:

    猜你喜欢
    • 2019-10-27
    • 1970-01-01
    • 2018-10-19
    • 2013-10-26
    • 1970-01-01
    • 2011-10-05
    • 2018-12-01
    • 2020-12-11
    • 2020-06-01
    相关资源
    最近更新 更多