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
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.
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");
...
Try to use QtCUrl. It's easy, if you are familiar with curl.
QtCUrl cUrl;
QUrl url("https://api.parse.com/1/login");
url.addEncodedQueryItem("username", "test");
url.addEncodedQueryItem("password", "test");
QtCUrl::Options opt;
opt[CURLOPT_URL] = url;
QStringList headers;
headers
<< "X-Parse-Application-Id: myappid"
<< "X-Parse-REST-API-Key: myapikey"
opt[CURLOPT_HTTPHEADER] = headers;
QString result = cUrl.exec(opt);