Search code examples
pythonhttp-post

How to fix a POST request that returns : "Invalid JSON primitive: ."


I am trying to scrape bets through a POST request that returns "Invalid JSON primitive: ."

All of the headers and the body that is sent is taken from the original already existent requests' cURL from the site. Since I couldn't find a solution i tried changing the type of the file that the POST request returns thinking that could fix it somehow but when i change it from JSON i just get the HTML body of the original site.

Website

Code:

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)

The error:

"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"

Solution

  • payload isn't valid JSON. Create it as a dictionary and let requests.request() format it as JSON by using json=payload

    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())