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 2.1k 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.
  • jsulmJ jsulm

    @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 last edited by
    #3

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

    jsulmJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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

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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on 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 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
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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
            0
            • Christian EhrlicherC Christian Ehrlicher

              @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 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
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 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
                3
                • Christian EhrlicherC Christian Ehrlicher

                  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 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 last edited by
                    #11

                    anyone? :)

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 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
                      0
                      • SGaistS SGaist

                        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 last edited by
                        #13

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

                        1 Reply Last reply
                        0
                        • SGaistS SGaist

                          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 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 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
                            1
                            • SGaistS SGaist

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

                              K Offline
                              K Offline
                              Kris Revi
                              wrote on 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 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
                                0
                                • SGaistS SGaist

                                  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 last edited by
                                  #18
                                  This post is deleted!
                                  1 Reply Last reply
                                  0

                                  • Login

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