sending json via QNetwork (using Python)
-
wrote on 25 Mar 2020, 11:42 last edited by Kris Revi
how do i send json via QNetwork? all i know is to set the header to "application/json"
from PyQt5 import QtCore, QtWidgets, QtNetwork from PyQt5.QtCore import QUrl, QTimer class software(QtWidgets.QMainWindow): def __init__(self, parent=None): QtWidgets.QWidget.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.nam = QtNetwork.QNetworkAccessManager() self.nam.finished.connect(self.handlePostResponse) self.doPost() def doPost(self): data = QtCore.QByteArray() data.append("selectedPattern=2") request = QtNetwork.QNetworkRequest(QtCore.QUrl("http://192.168.10.2/selectPattern")) request.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader, "application/x-www-form-urlencoded") self.nam.post(request, data) def handlePostResponse(self, reply): er = reply.error() if er == QtNetwork.QNetworkReply.NoError: print("No Error") print(reply.readAll()) else: print("Error occurred: ", er) print(reply.errorString()) if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) myapp = software() myapp.show() sys.exit(app.exec_())
-
how do i send json via QNetwork? all i know is to set the header to "application/json"
from PyQt5 import QtCore, QtWidgets, QtNetwork from PyQt5.QtCore import QUrl, QTimer class software(QtWidgets.QMainWindow): def __init__(self, parent=None): QtWidgets.QWidget.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.nam = QtNetwork.QNetworkAccessManager() self.nam.finished.connect(self.handlePostResponse) self.doPost() def doPost(self): data = QtCore.QByteArray() data.append("selectedPattern=2") request = QtNetwork.QNetworkRequest(QtCore.QUrl("http://192.168.10.2/selectPattern")) request.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader, "application/x-www-form-urlencoded") self.nam.post(request, data) def handlePostResponse(self, reply): er = reply.error() if er == QtNetwork.QNetworkReply.NoError: print("No Error") print(reply.readAll()) else: print("Error occurred: ", er) print(reply.errorString()) if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) myapp = software() myapp.show() sys.exit(app.exec_())
@Kris-Revi So, you want to send HTTP POST request? If so: https://doc.qt.io/qt-5/qnetworkaccessmanager.html#post-1 data (second parameter) is where you pass your JSON.
-
@Kris-Revi So, you want to send HTTP POST request? If so: https://doc.qt.io/qt-5/qnetworkaccessmanager.html#post-1 data (second parameter) is where you pass your JSON.
-
@Kris-Revi said in sending json via QNetwork:
yes and so just make data variable json with QJsonDocument?
And now please express this in a sentence which we are able to understand.
-
-
wrote on 25 Mar 2020, 15:35 last edited by
i've been googling my ass off but support for python Qt is small to non existent, can't find any example :/
-
@Kris-Revi said in sending json via QNetwork (using Python):
i've been googling my ass off but support for python Qt is small to non existent
And what problem do you have? You don't ask questions we really understand but we try to help as best as we can and then you ignore our links...
-
@Kris-Revi said in sending json via QNetwork (using Python):
i've been googling my ass off but support for python Qt is small to non existent
And what problem do you have? You don't ask questions we really understand but we try to help as best as we can and then you ignore our links...
wrote on 25 Mar 2020, 15:53 last edited by@Christian-Ehrlicher how do i make "doPost" send json like this
data = {"key": value1}
using QJsonDocument
-
Create a QJsonObject with the values you want, the use a QJsonDocument to convert it to a QByteArray. See https://doc.qt.io/qt-5/qtcore-serialization-savegame-example.html for an example.
-
Create a QJsonObject with the values you want, the use a QJsonDocument to convert it to a QByteArray. See https://doc.qt.io/qt-5/qtcore-serialization-savegame-example.html for an example.
wrote on 25 Mar 2020, 18:09 last edited by@Christian-Ehrlicher what am i doing wrong? :/
json = QJsonDocument.object() json["selectedPattern"] = "2" document = QJsonDocument(json) doc = document.toJson(document) print(doc)
Error :
object(self): first argument of unbound method must have type 'QJsonDocument'
-
wrote on 25 Mar 2020, 18:48 last edited by
anyone? :)
-
Please show some patience and allow at least 24 hours to pass before bumping your own thread. This forum is run by volunteers who may not live in the same timezone as you.
As for your error, did you check what it means ?
-
Please show some patience and allow at least 24 hours to pass before bumping your own thread. This forum is run by volunteers who may not live in the same timezone as you.
As for your error, did you check what it means ?
-
Please show some patience and allow at least 24 hours to pass before bumping your own thread. This forum is run by volunteers who may not live in the same timezone as you.
As for your error, did you check what it means ?
-
The QJsonDocument::toJson overload you want to use takes no argument.
-
The QJsonDocument::toJson overload you want to use takes no argument.
wrote on 25 Mar 2020, 20:18 last edited by@SGaist ok this is what i got sofar
def doPost(self): try: json = {'selectedPatter': '2'} document = QJsonDocument(json) print(document.toJson()) request = QtNetwork.QNetworkRequest(QtCore.QUrl("http://192.168.10.2/selectPattern")) request.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader, "application/json") self.nam2.post(request, document.toJson()) except Exception as e: print(e) def handlePostResponse(self, reply): er = reply.error() if er == QtNetwork.QNetworkReply.NoError: print("No Error") print(reply.readAll()) else: print("Error occurred: ", er) print(reply.errorString())
the print out in doPost function looks weird :S this b' and \n
b'{\n "selectedPatter": "2"\n}\n'
but the print in handlePostResponse function is
No Error b''
-
No it does not, it's a string, or rather bytes, containing a simple json document containing one field.
As for the reply you are getting, what are you expecting ? Is the service supposed to return something ?
-
No it does not, it's a string, or rather bytes, containing a simple json document containing one field.
As for the reply you are getting, what are you expecting ? Is the service supposed to return something ?
wrote on 25 Mar 2020, 20:44 last edited byThis post is deleted!
1/18