Issue
I am trying to create a simple rest api with QHttpServer in Python. I had checked examples in C++. However, I couldn't get it to work in Python. When callback_api
returns a string, it is visible as plain text, but any other return value creates a response with empty body and status code 200. Moreover status codes other than 200 do not have any effect. What is the correct way to return json and/or desired status code?
import sys
from PySide6.QtWidgets import QApplication
from PySide6.QtHttpServer import QHttpServer,QHttpServerRequest, QHttpServerResponse, QHttpServerResponder
from PySide6.QtNetwork import QHostAddress
from PySide6.QtCore import QJsonArray, QJsonValue
def test(req: QHttpServerRequest):
d = {"key": "value"}
return QHttpServerResponse(d)
app = QApplication(sys.argv)
server = QHttpServer()
server.listen(QHostAddress("127.0.0.1"), 5005)
server.route("/api", test)
app.exec()
Solution
You can't do that. Currently PySide does not support a view handler which returns a QHttpServerResponse
. (See the source for a detail.)
Either use Qt HTTP Server library in C++ or use other Python web frameworks such as Flask, FastAPI, etc.
Answered By - relent95
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.