tags:

views:

1761

answers:

3

How can I create a HTTP POST request with some URL encoded parameters using Qt 4.6.1?

I figured out that I can create a QNetworkRequest, set all the parameters there and send it via QNetworkAccessManagers post method. But how can I add some URL-encoded parameters to the request?

In the end I want to access the Eve API using Qt/C++. A Python example can be found here: http://www.eveonline.com/api/doc/example-python.asp


I managed it using something like (still to be refactored and formed into something useful):

QNetworkReply *requestApi(QNetworkAccessManager &nwam)
{

    QNetworkRequest request(QUrl("http://api.eve-online.com/account/Characters.xml.aspx"));
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");

    QByteArray data;
    QUrl params;

    params.addQueryItem("userid","user");
    params.addQueryItem("apiKey","key");
    data.append(params.toString());
    data.remove(0,1);

    QNetworkReply *reply = nwam.post(request,data);
    return reply;
}
+1  A: 

QUrl::addEncodedQueryItem() ?

I'm using Qt myself, but haven't been using the HTTP parts that much... yet.

Marcus Lindblom
+1  A: 

Hi,

I'm sorry that I only find your post this late. However, I'll still try to help, in case anyone else is searching for the answer.

By accident, I'm also working on an EVE API application, and I also tried the same way. Unfortunately, QNetworkManager doesn't work that way, because it posts the request asynchronously. You have to connect a slot to its finished(QNetworkReply*) signal.

I do it by making a request with a separate class called EveConnector, processing the reply in the slot connected to the QNetworkManager's finished signal, and then calling back the requesting object through the connector class's own signals.

I would happily share the code, if you ask.

Venemo
A: 

How I retrieve the response of post request? I tried

reply->waitForReadyRead(-1);
qDebug() << reply->readAll();

but got no success, any idea?

best regards, Vinicius.

Vinicius Cavalcanti
Only post an answer if it's an answer to the question asked (this doesn't work like a forum discussion). If you have your own individual question, create a question just for that, or if you want clarification on someone's answer, add a comment to that response.
Zurahn