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
New Developer
Posts: 13
Registered: ‎11-13-2013
My Device: Q10
My Carrier: Verizon
Accepted Solution

Help with HTTP Post JSON Request

Still fairly new to QT so I'm trying to send a http post request with some json, I'm pulling the json from a file and that seems to be working fine but I keep getting a http 500 error. I just want to ensure my code is correct before contacting the company who webservice I'm using here is my code:

 

  

 JsonDataAccess jda;
    QVariant list = jda.load(QDir::currentPath() +"/app/native/assets/jsonData/myjson.json");

    qDebug()<<list;

    QUrl webServiceUrl("https://thewebserviceurl/");
    QNetworkAccessManager* networkAccessManager = new QNetworkAccessManager(this);
    QNetworkRequest request (webServiceUrl);

    request.setRawHeader("Content-Type", "application/json");
    request.setRawHeader("Content-Length", QByteArray::number(list.toByteArray().size()));

    QNetworkReply *reply = networkAccessManager->post(request, list.toByteArray());

 

I have a feeling I'm passing in the json data into the post method incorrect. Any assistance is appreicated

Developer
Posts: 540
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....
My Carrier: Telekom Slovakia

Re: Help with HTTP Post JSON Request

[ Edited ]

Hi

You are sending an empty string to server because list.toByteArray() returning empty string.

 

You need save list to QByteArray;

 

QByteArray result;
jda.saveToBuffer(list, &result);

// and then

QNetworkReply *reply = networkAccessManager->post(request, result);

or

 

Just load the json file with QFile

QFile file(YOUR_JSON);
if (!file.open(QIODevice::ReadOnly)){
    qDebug() << Q_FUNC_INFO << file.errorString();
    return;
}
QByteArray result = file.readAll();
file.close()

QNetworkReply *reply = networkAccessManager->post(request, result);

 Hoe it helps :smileywink:

 



If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
New Developer
Posts: 13
Registered: ‎11-13-2013
My Device: Q10
My Carrier: Verizon

Re: Help with HTTP Post JSON Request

That did the trick thanks!