I have a question about making POST request in PyQt5. Unfortunately official documentation for this framework for Python doesen’t exist. I have to translate docs from C++ to Python.
I have a problem with handle it. To make POST request I have to create instance of class QWebEngineHttpRequest
(docs), and then add POST data by setPostData()
, it looks to be easy, but that method requires a parameter in type QByteArray
(docs), and here is a problem because i don’t know how to insert data into this.
Advertisement
Answer
I know I’m late, but I hope it helps someone else with the same issue. Here’s how I’ve done it:
JavaScript
x
18
18
1
def postRequest(self):
2
self.url = QUrl()
3
self.req = QWebEngineHttpRequest()
4
5
self.url.setScheme("http")
6
self.url.setHost("stackoverflow")
7
self.url.setPath("/something/somethingelse")
8
9
self.req.setUrl(self.url)
10
self.req.setMethod(QWebEngineHttpRequest.Post)
11
self.req.setHeader(QByteArray(b'Content-Type'),QByteArray(b'application/json'))
12
13
params = {"something": value, "test": True, "number": 5}
14
15
self.req.setPostData(bytes(json.dumps(params), 'utf-8'))
16
17
return self.req
18