Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 15
Registered: ‎01-02-2012
My Device: playbook
My Carrier: digitel
Accepted Solution

GET HTTP REQUEST PARAMETERS

Hello

 

How can i send three parameters via GET to my server, i need to send three sttrings generated in my app to the server.

 

I tried with network request whit no succes, the request goes but the parameters never arrive 

 


QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkRequest request(QUrl("http://server.php"));
request.setRawHeader("to","email");
request.setRawHeader("title","hello");
request.setRawHeader("msg","text msg");

manager->get(request);

 

Thank you

Developer
Posts: 21
Registered: ‎04-03-2013
My Device: BlackBerry Passport (SQW100-1)
My Carrier: o2 (Telefónica Germany)

Re: GET HTTP REQUEST PARAMETERS

You could add the strings as GET parameters to the URL:

 

QUrl("http://server.php?to=email&title=hello&msg=text+msg");

 

But I think you should POST these fields to the server because URLs have a maximum length:

 

QByteArray data;

data.append("to=foo@bar.com&");

data.append("title=hello world&");

data.append("msg=hello world! how are you?");

manager->post(request, data);

 

I've wrapped this into a method, that takes QMap<QString,QString> as parameter and returns QByteArray, because it's quite annoying to keep in mind to add "&" between each key-value pair.

Software Release: 10.3.1.1779
Developer
Posts: 293
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: GET HTTP REQUEST PARAMETERS

You can use QUrl::addQueryItem

QUrl url("http://server.php");

qurl.addQueryItem("to", email);
qurl.addQueryItem("title", "hello");

QNetworkRequest request(url);
manager->get(request);

addQueryItem will append the variables to the URL

You might also want to consider a POST instead of a GET - especially if you are sending data to a server rather than retrieving a resource. You can addQueryItem in the same way with a POST, and additional you can also add data to the POST body:

QUrl url("http://server.php");

// these go over the URL
qurl.addQueryItem("to", email);
qurl.addQueryItem("title", "hello");

// this goes in the message body
QString message = "your mail message";
manager->post(request, message.toUtf8());
Developer
Posts: 21
Registered: ‎04-03-2013
My Device: BlackBerry Passport (SQW100-1)
My Carrier: o2 (Telefónica Germany)

Re: GET HTTP REQUEST PARAMETERS

Cool, I didn't knew QUrlQuery::addQueryItem()! Using this method is much safer than concatenating strings to get an URL with GET-parameters.

Thanks!
Software Release: 10.3.1.1779
Highlighted
Developer
Posts: 15
Registered: ‎01-02-2012
My Device: playbook
My Carrier: digitel

Re: GET HTTP REQUEST PARAMETERS

Thank you for your help guys!