QHttpServer, GET vs POST
-
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 = "<html><head><title>Greeting App</title></head><body>"; risposta_http += "<b>YOUR IP: " + req->remoteAddress() + "<br><br></b>"; risposta_http += "<form name=\"input\" action=\"\" method=\"GET\">" "Username: <input type=\"text\" name=\"user\">" "<input type=\"submit\" value=\"Submit\">" "</form> "; risposta_http += "</body></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
-
What is the value of "req->body().size()"?
req->body() is a QByteArray. So, you need to parse the contents.
-
[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...