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. QHttpServer, GET vs POST
Forum Updated to NodeBB v4.3 + New Features

QHttpServer, GET vs POST

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 4.6k 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.
  • L Offline
    L Offline
    luca
    wrote on last edited by
    #1

    Hi all, I'm trying to put a simple HTTP server in my Qt5 application.
    I'm actually using QHttpServer because of it's semplicity.

    This is the code:
    @
    ...
    QHttpServer server = new QHttpServer;
    server->listen(QHostAddress::Any, 5001);
    connect(server, SIGNAL(newRequest(QHttpRequest
    , QHttpResponse*)),
    this, SLOT(handle(QHttpRequest*, QHttpResponse*)));
    ...

    void MyObject::handle(QHttpRequest *req, QHttpResponse *resp)
    {
    qDebug() << req->path();
    qDebug() << req->remoteAddress();
    qDebug() << req->methodString();
    qDebug() << req->body().size();
    QUrlQuery url_query(req->url());
    qDebug() << url_query.queryItems();

    QString risposta_http;
    risposta_http = "&lt;html&gt;&lt;head>&lt;title&gt;Greeting App&lt;/title&gt;&lt;/head>&lt;body&gt;";
    risposta_http += "<b>YOUR IP: " + req->remoteAddress() + "<br><br></b>";
    risposta_http += "&lt;form name=\"input\" action=\"\" method=\"GET\"&gt;"
            "Username: &lt;input type=\"text\" name=\"user\"&gt;"
            "&lt;input type=\"submit\" value=\"Submit\"&gt;"
            "&lt;/form&gt; ";
    
    risposta_http += "&lt;/body&gt;&lt;/html>";
    resp->setHeader("Content-Length", QString::number(risposta_http.size()));
    resp->writeHead(200);
    resp->write(risposta_http);
    resp->end();
    

    }
    @

    This way when I point my browser to: localhost:5001 I get a simple HTTP form where I can put my name.
    The first time I connect to the web server I get the following "debug messages" in the top of "handle()":
    @
    "/"
    "127.0.0.1"
    "HTTP_GET"
    0
    ()
    @
    Then if I enter a name "Luca" and press "submit" in the browser I get this debugs:
    @
    "/"
    "127.0.0.1"
    "HTTP_GET"
    0
    (QPair("user","Luca") )
    @
    and this is what I expect.

    If I user POST instead of GET:
    @
    risposta_http += "<form name="input" action="" method="POST">"
    "Username: <input type="text" name="user">"
    "<input type="submit" value="Submit">"
    "</form> ";
    @

    I can't get the name I insert in the form:
    @
    (QPair("user","Luca") )
    @

    Can someone tell me why?

    Thanks

    1 Reply Last reply
    0
    • T Offline
      T Offline
      TioRoy
      wrote on last edited by
      #2

      What is the value of "req->body().size()"?

      req->body() is a QByteArray. So, you need to parse the contents.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        luca
        wrote on last edited by
        #3

        [quote author="TioRoy" date="1378267553"]What is the value of "req->body().size()"?

        req->body() is a QByteArray. So, you need to parse the contents.[/quote]

        Hi, as you see in the output I posted:
        @
        "/"
        "127.0.0.1"
        "HTTP_GET"
        0
        (QPair("user","Luca") )
        @

        the output of req->body().size() is 0 so there is nothing in it.

        This is strange because in QHttpServer source there is:
        @
        /*!
        * Post data
        */
        const QByteArray &body() const { return m_body; }
        @

        so I think this is useful when using POST method...

        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