Solved sending json via QNetwork (using Python)
-
@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...
-
@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.
-
@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'
-
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 ?
-
@SGaist ok sorry for that :) i'll keep that in mind!
-
@SGaist i tried googling but didn't find anything on the error :S
-
The QJsonDocument::toJson overload you want to use takes no argument.
-
@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 ?
-
This post is deleted!