'Why the error occured when i tried to send a POST request with QWebEngineHttpRequest (PyQt5)?

I try to get JSON Object from source, but getting only errors. Now I'm trying to understand what I'm doing wrong. This code works well with other resources. Maybe It`s not problem with code, but with web-source.

import sys
import json
from PyQt5.QtCore import QByteArray, QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineCore import QWebEngineHttpRequest
from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineSettings
from pprint import pprint


def on_load_finished():
    """Handle response"""
    engine.toPlainText(handle_to_html)  # get json


def handle_to_html(html):
    """Handle html"""
    print(html)
    QApplication.quit()


if __name__ == "__main__":

    app = QApplication(sys.argv)
    engine = QWebEnginePage()
    url = "https://sportsapi.betway.com/api/Events/V2/GetEvents"
    request = QWebEngineHttpRequest(url=QUrl(url), method=QWebEngineHttpRequest.Post)
    request.setHeader(QByteArray(b"Content-Type"), QByteArray(b"application/json"))

    payload = {
        "LanguageId": 1,
        "ClientTypeId": 2,
        "BrandId": 3,
        "JurisdictionId": 1,
        "ClientIntegratorId": 1,
        "ExternalIds": [9032262, 9038528, 9037778],
        "MarketCName": "win-draw-win",
        "ScoreboardRequest": {"ScoreboardType": 3, "IncidentRequest": {}},
        "BrowserId": 3,
        "OsId": 3,
        "ApplicationVersion": "",
        "BrowserVersion": "97.0.4692.99",
        "OsVersion": "NT 10.0",
        "SessionId": "null",
        "TerritoryId": 227,
        "CorrelationId": "06779075-21e2-4ba8-8e91-d71a981621fe",
        "VisitId": "d1088cdf-13a8-42d0-be90-b34fd1332c36",
        "ViewName": "sports",
        "JourneyId": "833a4c0c-3354-499f-9d52-949df6d159f9",
    }
    request.setPostData(bytes(json.dumps(payload), "utf-8"))
    engine.load(request)
    engine.loadFinished.connect(on_load_finished)
    app.exec_()

Errors are looks like

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<Error>
<Message>An error has occurred.</Message>
</Error>

Maybe It`s not problem with code, but with web-source.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source