Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. post to http [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

post to http [SOLVED]

Scheduled Pinned Locked Moved Mobile and Embedded
8 Posts 3 Posters 2.3k Views 3 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.
  • M Offline
    M Offline
    mkdh
    wrote on last edited by mkdh
    #1

    How to post some request command and file to a link?
    I have a link "http://192.168.100.1/".
    tha form have id='Ssid' value.
    My code :
    QByteArray mydata ;

        QNetworkRequest request(QUrl("http://192.168.100.1/"));
        mydata.append("Ssid=QQ");
        mydata.append(file.readAll());
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
        manager->post(request,mydata);
    

    But, I can't recevie mydata from the host?!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      have you checked if you get some errors?

      QNetworkReply *reply = manager->post(request, mydata);
      
      connect(reply, SIGNAL(error(QNetworkReply::NetworkError code)), this, SLOT(handleError(QNetworkReply::NetworkError code)));
      
      void MyClass::handleError(QNetworkReply::NetworkError code)
      {
      ....
      }
      

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      M 1 Reply Last reply
      0
      • M mcosta

        Hi,

        have you checked if you get some errors?

        QNetworkReply *reply = manager->post(request, mydata);
        
        connect(reply, SIGNAL(error(QNetworkReply::NetworkError code)), this, SLOT(handleError(QNetworkReply::NetworkError code)));
        
        void MyClass::handleError(QNetworkReply::NetworkError code)
        {
        ....
        }
        
        M Offline
        M Offline
        mkdh
        wrote on last edited by mkdh
        #3

        @mcosta

        "Ssid" is my command type for request

        and I find that "mydata.append("Ssid=QQ");" is illegal for my Qt 5.4. ="=

        I must declare QUrlQuery query.

        and the statement should be "qrery.addQueryItem( "Ssid", "QQ")"......

        and url.setQuery(query).

        Is it legal statement before?
        Or I could use "mydata.append("Ssid=QQ");" being my request command variable in some way?

        p3c0P 1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          Hi,

          the URL must be encoded ("application/x-www-form-urlencoded") to be sent correctly using HTTP (addQueryItem does it for you).

          I think it's better to use QUrlQuery in order to handle correctly encodig

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • M mkdh

            @mcosta

            "Ssid" is my command type for request

            and I find that "mydata.append("Ssid=QQ");" is illegal for my Qt 5.4. ="=

            I must declare QUrlQuery query.

            and the statement should be "qrery.addQueryItem( "Ssid", "QQ")"......

            and url.setQuery(query).

            Is it legal statement before?
            Or I could use "mydata.append("Ssid=QQ");" being my request command variable in some way?

            p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by p3c0
            #5

            Hi @mkdh I don't think there is any problem with append method.
            Is the url and the port correct ? By default it will hit port 80.
            Also is there a POST handler on server side ?

            157

            M 1 Reply Last reply
            0
            • p3c0P p3c0

              Hi @mkdh I don't think there is any problem with append method.
              Is the url and the port correct ? By default it will hit port 80.
              Also is there a POST handler on server side ?

              M Offline
              M Offline
              mkdh
              wrote on last edited by mkdh
              #6

              Hi @p3c0 ,
              I don't think there is any problem with append method too.

              But, In my opinion, if you want to get the request command type or value, you must use setQuery method.

              Or could I use "mydata.append("Ssid=QQ");" being my request command variable in other way?

              p3c0P 1 Reply Last reply
              0
              • M mkdh

                Hi @p3c0 ,
                I don't think there is any problem with append method too.

                But, In my opinion, if you want to get the request command type or value, you must use setQuery method.

                Or could I use "mydata.append("Ssid=QQ");" being my request command variable in other way?

                p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by p3c0
                #7

                @mkdh Did you check url and port ?

                could I use "mydata.append("Ssid=QQ");" being my request command variable in other way?

                I didn't quite understand this.
                What is the problem exactly ? The data ("SSid=QQ") is not received on server side ?

                I just executed your code and using python on server side. Well, just modified the url, port and IP address. It works perfectly. Here's the python code:

                //myserver.py
                import web
                
                urls = (
                    "/get", "get",
                    "/post", "post"
                )
                app = web.application(urls, globals())
                
                class get:
                    def GET(self):
                        return "get"
                
                class post:
                    def POST(self):
                        data = web.data()
                        print data // <- This prints the data which we POST'ed from Qt
                        return "post"
                
                if __name__ == "__main__":
                    app.run()
                

                This runs by default on http://0.0.0.0:8080/ ( to execute just run python myserver.py ) and on Qt side

                QByteArray mydata ;
                QNetworkRequest request(QUrl("http://0.0.0.0:8080/post"));
                mydata.append("Ssid=QQ");
                mydata.append(file.readAll());
                

                You can try running it if you are familiar with python. As @mcosta mentioned earlier did you get any errors ?

                157

                M 1 Reply Last reply
                0
                • p3c0P p3c0

                  @mkdh Did you check url and port ?

                  could I use "mydata.append("Ssid=QQ");" being my request command variable in other way?

                  I didn't quite understand this.
                  What is the problem exactly ? The data ("SSid=QQ") is not received on server side ?

                  I just executed your code and using python on server side. Well, just modified the url, port and IP address. It works perfectly. Here's the python code:

                  //myserver.py
                  import web
                  
                  urls = (
                      "/get", "get",
                      "/post", "post"
                  )
                  app = web.application(urls, globals())
                  
                  class get:
                      def GET(self):
                          return "get"
                  
                  class post:
                      def POST(self):
                          data = web.data()
                          print data // <- This prints the data which we POST'ed from Qt
                          return "post"
                  
                  if __name__ == "__main__":
                      app.run()
                  

                  This runs by default on http://0.0.0.0:8080/ ( to execute just run python myserver.py ) and on Qt side

                  QByteArray mydata ;
                  QNetworkRequest request(QUrl("http://0.0.0.0:8080/post"));
                  mydata.append("Ssid=QQ");
                  mydata.append(file.readAll());
                  

                  You can try running it if you are familiar with python. As @mcosta mentioned earlier did you get any errors ?

                  M Offline
                  M Offline
                  mkdh
                  wrote on last edited by mkdh
                  #8

                  Hi @p3c0 ,
                  I use Qt 5.4 for IOS/android. And install my program to my Android Phone.
                  My partner use IAR to detect my request on server side.
                  He told me that he don't receive my "Ssid" command from request link.
                  But I had solved this problem by using setQuery method.
                  Thank for you reply.^^

                  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