[SOLVED] HTTP POST failed



  • 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



  • 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



  • 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


  • Lifetime Qt Champion

    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 ?



  • 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


  • Lifetime Qt Champion

    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.



  • 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


  • Lifetime Qt Champion

    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.



  • 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


  • Lifetime Qt Champion

    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 ?



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.