如何修复返回:“无效的 JSON 原语:”的 POST 请求

问题描述 投票:0回答:1

我正在尝试通过返回“无效的 JSON 原语:”的 POST 请求来抓取赌注

发送的所有标头和正文均取自站点中原始已存在请求的 cURL。由于我找不到解决方案,我尝试更改 POST 请求返回的文件类型,认为可以以某种方式修复它,但是当我从 JSON 更改它时,我只得到原始站点的 HTML 正文。

网站

代码:

import requests
import json

url = "https://www.starbet.rs/Oblozuvanje.aspx/GetLiga"

payload = "{LigaID:^[494^],filter:0,parId:0}"
###LigaID is the ID of the chosen leagues not really of importance
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0",
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "Accept-Language": "en-US,en;q=0.5",
    "Accept-Encoding": "gzip, deflate, br",
    "Content-Type": "application/json; charset=utf-8",
    "X-Requested-With": "XMLHttpRequest",
    "Origin": "https://www.starbet.rs",
    "DNT": "1",
    "Sec-GPC": "1",
    "Connection": "keep-alive",
    "Referer": "https://www.starbet.rs/Bet",
    "Cookie": "ASP.NET_SessionId=jsjbtw2gizig2sh3mfrrqyje; style=null",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-origin",
    "TE": "trailers"
}

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

错误:

"Message": "Invalid JSON primitive: .",
    "StackTrace": "   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)",
    "ExceptionType": "System.ArgumentException"
python http-post
1个回答
0
投票

payload
不是有效的 JSON。将其创建为字典,并让
requests.request()
使用
json=payload

将其格式化为 JSON
import requests

url = "https://www.starbet.rs/Oblozuvanje.aspx/GetLiga"

payload = {"LigaID": "^[494^]", "filter": 0, "parId": 0}
###LigaID is the ID of the chosen leagues not really of importance
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0",
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "Accept-Language": "en-US,en;q=0.5",
    "Accept-Encoding": "gzip, deflate, br",
    "Content-Type": "application/json; charset=utf-8",
    "X-Requested-With": "XMLHttpRequest",
    "Origin": "https://www.starbet.rs",
    "DNT": "1",
    "Sec-GPC": "1",
    "Connection": "keep-alive",
    "Referer": "https://www.starbet.rs/Bet",
    "Cookie": "ASP.NET_SessionId=jsjbtw2gizig2sh3mfrrqyje; style=null",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-origin",
    "TE": "trailers"
}

response = requests.request("POST", url, json=payload, headers=headers)

print(response.json)
© www.soinside.com 2019 - 2024. All rights reserved.