Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. sending json via QNetwork (using Python)
Forum Updated to NodeBB v4.3 + New Features

sending json via QNetwork (using Python)

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kris Revi
    wrote on 25 Mar 2020, 11:42 last edited by Kris Revi
    #1

    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_())
    
    J 1 Reply Last reply 25 Mar 2020, 12:23
    0
    • K Kris Revi
      25 Mar 2020, 11:42

      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_())
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 25 Mar 2020, 12:23 last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply 25 Mar 2020, 12:29
      2
      • J jsulm
        25 Mar 2020, 12:23

        @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.

        K Offline
        K Offline
        Kris Revi
        wrote on 25 Mar 2020, 12:29 last edited by
        #3

        @jsulm yes and so just make data variable json with QJsonDocument?

        J 1 Reply Last reply 25 Mar 2020, 12:38
        0
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 25 Mar 2020, 12:37 last edited by
          #4

          @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.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • K Kris Revi
            25 Mar 2020, 12:29

            @jsulm yes and so just make data variable json with QJsonDocument?

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 25 Mar 2020, 12:38 last edited by
            #5

            @Kris-Revi https://doc.qt.io/qt-5/qjsondocument.html#toJson

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • K Offline
              K Offline
              Kris Revi
              wrote on 25 Mar 2020, 15:35 last edited by
              #6

              i've been googling my ass off but support for python Qt is small to non existent, can't find any example :/

              1 Reply Last reply
              -1
              • C Online
                C Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 25 Mar 2020, 15:50 last edited by
                #7

                @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...

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                K 1 Reply Last reply 25 Mar 2020, 15:53
                0
                • C Christian Ehrlicher
                  25 Mar 2020, 15:50

                  @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...

                  K Offline
                  K Offline
                  Kris Revi
                  wrote on 25 Mar 2020, 15:53 last edited by
                  #8

                  @Christian-Ehrlicher how do i make "doPost" send json like this

                  data = {"key": value1}
                  

                  using QJsonDocument

                  1 Reply Last reply
                  0
                  • C Online
                    C Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 25 Mar 2020, 16:07 last edited by
                    #9

                    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.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    K 1 Reply Last reply 25 Mar 2020, 18:09
                    3
                    • C Christian Ehrlicher
                      25 Mar 2020, 16:07

                      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.

                      K Offline
                      K Offline
                      Kris Revi
                      wrote on 25 Mar 2020, 18:09 last edited by
                      #10

                      @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'
                      
                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        Kris Revi
                        wrote on 25 Mar 2020, 18:48 last edited by
                        #11

                        anyone? :)

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 25 Mar 2020, 18:58 last edited by
                          #12

                          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 ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          K 2 Replies Last reply 25 Mar 2020, 19:07
                          0
                          • SGaistS SGaist
                            25 Mar 2020, 18:58

                            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 ?

                            K Offline
                            K Offline
                            Kris Revi
                            wrote on 25 Mar 2020, 19:07 last edited by
                            #13

                            @SGaist ok sorry for that :) i'll keep that in mind!

                            1 Reply Last reply
                            0
                            • SGaistS SGaist
                              25 Mar 2020, 18:58

                              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 ?

                              K Offline
                              K Offline
                              Kris Revi
                              wrote on 25 Mar 2020, 19:22 last edited by
                              #14

                              @SGaist i tried googling but didn't find anything on the error :S

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 25 Mar 2020, 19:58 last edited by
                                #15

                                The QJsonDocument::toJson overload you want to use takes no argument.

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                K 1 Reply Last reply 25 Mar 2020, 20:18
                                1
                                • SGaistS SGaist
                                  25 Mar 2020, 19:58

                                  The QJsonDocument::toJson overload you want to use takes no argument.

                                  K Offline
                                  K Offline
                                  Kris Revi
                                  wrote on 25 Mar 2020, 20:18 last edited by
                                  #16

                                  @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''
                                  
                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on 25 Mar 2020, 20:37 last edited by
                                    #17

                                    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 ?

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    K 1 Reply Last reply 25 Mar 2020, 20:44
                                    0
                                    • SGaistS SGaist
                                      25 Mar 2020, 20:37

                                      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 ?

                                      K Offline
                                      K Offline
                                      Kris Revi
                                      wrote on 25 Mar 2020, 20:44 last edited by
                                      #18
                                      This post is deleted!
                                      1 Reply Last reply
                                      0

                                      1/18

                                      25 Mar 2020, 11:42

                                      • Login

                                      • Login or register to search.
                                      1 out of 18
                                      • First post
                                        1/18
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved