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. [SOLVED] HTTP POST failed

[SOLVED] HTTP POST failed

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 3.9k Views
  • 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
    moravas
    wrote on 13 Nov 2015, 20:09 last edited by moravas 12 Feb 2015, 08:29
    #1

    Hi Folks,

    I would like to send HTTP POST request via Qt framework with additional data. This is my code:
    req.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");
    _networkManager.post(req, data);

    but the remote pair doesn't get data holds by "data" member. I tried to send same request via another, third party application, and in this case, the server receives everything,

    Can anybody help me, what did I wrong?

    Regards,
    Norbert

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 13 Nov 2015, 21:56 last edited by
      #2

      Hi,

      could you show how you build data and req?
      Is the event loop active?

      More code you show more help we can give you

      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 Offline
        M Offline
        moravas
        wrote on 28 Nov 2015, 09:12 last edited by moravas
        #3

        Hi,

        sorry for the late reply, I was quite busy
        the data is coming from the call:
        QString(QString::number(rowIndex, 16) + '=' + QString::number(state)).toUtf8();
        so as you can see, there is a string and query its UTF8 representation.

        the request is created as:
        QNetworkRequest req(url);
        where the url:
        const QUrl url("http://192.168.1.10:80/host/0x00");

        I see on the server side that the header of the request is arrived:
        GET /host?0x03=0 HTTP/1.1
        Connection: Keep-Alive
        Accept-Encoding: gzip, deflate
        Accept-Language: hu-HU,en,*
        User-Agent: Mozilla/5.0
        Host: 192.168.1.10:80

        but, as you can see, there is no content.

        can anybody help me?

        Regards,
        Norbert

        Update:

        I create a real minimal program, which one the issue is reproducible:
        QNetworkAccessManager manager;
        const QUrl url("http://192.168.1.10:80/host/0x00");
        QNetworkRequest req(url);
        req.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");
        manager.post(req, QString("1=1").toUtf8());

        On the server side, the output:
        POST /host/0x00 HTTP/1.1
        Content-Type: application/octet-stream
        Content-Length: 3
        Connection: Keep-Alive
        Accept-Encoding: gzip, deflate
        Accept-Language: hu-HU,en,*
        User-Agent: Mozilla/5.0
        Host: 192.168.1.10:80

        Please note, that the double new line is there at the end of the frame.

        Can anybody help me?

        Regards,
        Norbert

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 28 Nov 2015, 21:55 last edited by
          #4

          Hi,

          What does your server expect exactly ?

          Did you compare the content of the request made by your third party software and the one you make with Qt ?

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

          1 Reply Last reply
          0
          • M Offline
            M Offline
            moravas
            wrote on 29 Nov 2015, 10:17 last edited by
            #5

            Hi,

            so, what the Qt sends:
            POST /host/0x00 HTTP/1.1
            Content-Type: application/octet-stream
            Content-Length: 3
            Connection: Keep-Alive
            Accept-Encoding: gzip, deflate
            Accept-Language: hu-HU,en,*
            User-Agent: Mozilla/5.0
            Host: 192.168.1.10:80

            Please consider that the content length is filled out correctly, but there is no content.
            What the server expect:
            POST /host/0x00 HTTP/1.1
            Content-Type: application/octet-stream
            Content-Length: 3
            Connection: Keep-Alive
            Accept-Encoding: gzip, deflate
            Accept-Language: hu-HU,en,*
            User-Agent: Mozilla/5.0
            Host: 192.168.1.10:80

            1=1

            Please consider the data record at the end of the request.

            Regards,
            Norbert

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 29 Nov 2015, 21:28 last edited by
              #6

              Can you share the real code you use ? The last one looked like using only local variable which might get destroyed before everything is done.

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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                moravas
                wrote on 30 Nov 2015, 18:06 last edited by
                #7

                Hi,

                I wrote a really minimal test code, where I can verify it (sorry, on this new portal, I don't know, how can I highlight the code):

                QByteArray data(QString("1=1").toUtf8());
                int main(int argc, char *argv[])
                {
                QCoreApplication a(argc, argv);

                QNetworkAccessManager manager;
                const QUrl url("http://192.168.1.10:80/host/0x00");
                QNetworkRequest req(url);
                req.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");
                manager.post(req, data);
                
                return a.exec();
                

                }

                AS you can see, I'm using globally declared data and the application won't return until I don't close it (due to a.exec() call) so it has enough time to send everything.
                The result, what the server is getting:
                POST /host/0x00 HTTP/1.1
                Content-Type: application/octet-stream
                Content-Length: 3
                Connection: Keep-Alive
                Accept-Encoding: gzip, deflate
                Accept-Language: hu-HU,en,*
                User-Agent: Mozilla/5.0
                Host: 192.168.1.10:80

                Please, consider that there is a newline at the end of the "Host" header, and the content length is filled out correctly.

                Regards,
                Norbert

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 30 Nov 2015, 22:52 last edited by
                  #8

                  One thing I can see, you don't check whether the request is successful or not. I'd check that first.

                  On a side note, you don't need to pass by QSring to create your QByteArray: QByteArray payload("1=1"); is better in your case.

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

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    moravas
                    wrote on 1 Dec 2015, 17:17 last edited by
                    #9

                    Hi,

                    thanks for the guiding, I found my mistake: it seems, that Qt delivers post requests in multiple packages, which isn't already handled by my device (the server).

                    Maybe, do you know, how can I force Qt to send the request in one package? Is there a possibility to force it?

                    Regards,
                    Norbert

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 1 Dec 2015, 21:23 last edited by
                      #10

                      No I don't, TCP transmissions by definition can be split in several packets. What server are you using that doesn't handle that kind of communication ?

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

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        moravas
                        wrote on 2 Dec 2015, 08:29 last edited by
                        #11

                        Hi,

                        it didn't handle until now. Its an embedded device based on FreeRTOS, so every server features had to be developed by myself.

                        Regards,
                        Norbert

                        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