Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting http get request parameters using Qt

I'm developing a basic application in Qt that retrieves data from Parse.com using the REST API. I went through some class references and the cURL manual but it's still not clear how you set the request parameters. For example, I'd like to authenticate a user. Here's the curl example provided by Parse:

curl -X GET \
-H "X-Parse-Application-Id: myappid" \
-H "X-Parse-REST-API-Key: myapikey" \
-G \
--data-urlencode 'username=test' \
--data-urlencode 'password=test' \
https://api.parse.com/1/login

I set the url and the headers like this

QUrl url("https://api.parse.com/1/login");
QNetworkRequest request(url);

request.setRawHeader("X-Parse-Application-Id", "myappid");
request.setRawHeader("X-Parse-REST-API-Key", "myapikey");

nam->get(request);

which worked fine when there were no parameters, but what should I use to achieve the same as curl does with the --data-urlencode switch?

Thanks for your time

like image 712
Peter Avatar asked Mar 25 '13 15:03

Peter


2 Answers

Unfortunately, QUrl::addQueryItem() is deprecated in qt5 but starting from there I found the QUrlQuery class which has an addQueryItem() method and can produce a query string that is acceptable for QUrl's setQuery() method so it now looks like this and works fine:

QUrl url("https://api.parse.com/1/login");
QUrlQuery query;

query.addQueryItem("username", "test");
query.addQueryItem("password", "test");

url.setQuery(query.query());

QNetworkRequest request(url);

request.setRawHeader("X-Parse-Application-Id", "myappid");
request.setRawHeader("X-Parse-REST-API-Key", "myapikey");

nam->get(request);

Thanks for the tip Chris.

like image 134
Peter Avatar answered Sep 22 '22 21:09

Peter


I believe QUrl::addQueryItem() is what you're looking for

QUrl url("https://api.parse.com/1/login");
url.addQueryItem("username", "test");
url.addQueryItem("password", "test");
...
like image 40
Chris Avatar answered Sep 24 '22 21:09

Chris