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
RileyGB
Posts: 282
Registered: ‎08-06-2010
My Device: Z10
Accepted Solution

Sending data via HTTP post

Hey guys, I've been fighting with this one throughout the morning and have tried various samples to no avail.

 

I have a service which I need to post parameters to and then receive JSON data back. I'm having issues getting any of the post data to get through to the service (always comes up empty). I can't seem to find any information about POSTs anywhere on the developer portal either.

 

Here's the relevant code

 

    
	// Network manager
	mAccessManager = new QNetworkAccessManager();
       if (!mAccessManager)
       {
           qDebug() << "Unable to create QNetworkAccessManager!";
           return;
       }

	//  Handle SSL errors
   	connect(mAccessManager,
         SIGNAL(sslErrors ( QNetworkReply * , const QList<QSslError> & )), this,
         SLOT(onSslErrors ( QNetworkReply * , const QList<QSslError> & )));

    // Convert to string
    QByteArray byteArrayObject;
    byteArrayObject.append("name1=value1");

// Path to server
QString path = "http://myurl.com/post.php"; // Build request QUrl serviceURL(path); QNetworkRequest request(serviceURL); // Post and connect mReply = mAccessManager->post(request, byteArrayObject); connect(mReply, SIGNAL(finished()), this, SLOT(onFinished()));

 Any help / suggestions would be greatly appreciated, thanks.



Follow me on twitter @RileyGB - https://twitter.com/RileyGB
View my BB10 OpenSource projects - https://github.com/RileyGB/BlackBerry10-Samples
Please use plain text.
Developer
noahhuang
Posts: 100
Registered: ‎10-01-2012
My Device: BB10 Dev Alpha
My Carrier: Roger

Re: Sending data via HTTP post

[ Edited ]

in your class header .h file

 

public slots:
void onFinished();

 

 

in your class .cpp file

 

void YouClassName::smileysurprised:nFinished()
{
m_Reply = qobject_cast<QNetworkReply*>(sender());
QString response;
if (m_Reply)
{
if (m_Reply->error() == QNetworkReply::NoError)
{
int available = m_Reply->bytesAvailable();
if (available > 0)
{
qDebug() << m_Reply->readAll();
}
}
else
{
response =
QString("Error: ") + m_Reply->errorString()
+ QString(" status:")
+ m_Reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString();
qDebug() << response;
}
m_Reply->deleteLater();
}
if (response.trimmed().isEmpty())
{
response = "Unable to retrieve response";
}
emit completeUpdateVehicleImage(response);
}

Please use plain text.
Developer
RileyGB
Posts: 282
Registered: ‎08-06-2010
My Device: Z10

Re: Sending data via HTTP post

[ Edited ]

That appears to be response handling code rather than sending data.



Follow me on twitter @RileyGB - https://twitter.com/RileyGB
View my BB10 OpenSource projects - https://github.com/RileyGB/BlackBerry10-Samples
Please use plain text.
Developer
noahhuang
Posts: 100
Registered: ‎10-01-2012
My Device: BB10 Dev Alpha
My Carrier: Roger

Re: Sending data via HTTP post

This is parts of my codes to send json to server:

void WebService::smileytongue:ost(QNetworkReply* reply, QString action, QByteArray body)
{
if (!netManager)
{
qDebug() << "Unable to create QNetworkAccessManager!";
emit complete("Unable to create QNetworkAccessManager!");
}
QString urlString = QString("%1%2%3").arg(m_Env->getWebServerUri()).arg(m_Env->getServiceUri()).arg(action);
QUrl url(urlString);
QNetworkRequest req(url);
req.setUrl(QUrl(urlString));
req.setRawHeader("Accept", "application/json");
req.setRawHeader("Content-Type", "application/json");
req.setRawHeader("Authorization", "Basic " + QByteArray(QString("%1:%2").arg(user).arg(password).toAscii()).toBase64());

qDebug()<<"post: "<<urlString;
qDebug()<<"data: "<<QString(body);

reply = netManager->post(req, body);
}
Please use plain text.
Developer
RileyGB
Posts: 282
Registered: ‎08-06-2010
My Device: Z10

Re: Sending data via HTTP post

[ Edited ]

Thanks for the replies :smileyhappy:

 

 

I'm still not receiving any body at all server side. Any thoughts?



Follow me on twitter @RileyGB - https://twitter.com/RileyGB
View my BB10 OpenSource projects - https://github.com/RileyGB/BlackBerry10-Samples
Please use plain text.
Developer
RileyGB
Posts: 282
Registered: ‎08-06-2010
My Device: Z10

Re: Sending data via HTTP post

I've finally got this figured out.

 

I was missing a few important lines:

 

    // For your "Content-Length" header
    QByteArray postDataSize = QByteArray::number(body.size());

 

   // Set Headers
    request.setHeader( QNetworkRequest::ContentTypeHeader,"application/json" );
    request.setHeader( QNetworkRequest::ContentLengthHeader,QString(postDataSize).toUtf8() );

 

Below is a full example.

 

 

	// Network manager
	mAccessManager = new QNetworkAccessManager();
       if (!mAccessManager)
       {
           qDebug() << "Unable to create QNetworkAccessManager!";
           return;
       }

	//  Handle SSL errors
   	connect(mAccessManager,
         SIGNAL(sslErrors ( QNetworkReply * , const QList<QSslError> & )), this,
         SLOT(onSslErrors ( QNetworkReply * , const QList<QSslError> & )));

    // Convert to string
    QByteArray byteArrayObject;
    byteArrayObject.append("name1=value1");

    // For your "Content-Length" header
    QByteArray postDataSize = QByteArray::number(body.size());

    // Path to server
    QString path = "http://myurl.com/post.php";

    // Build request
    QUrl serviceURL(path);
    QNetworkRequest request(serviceURL);

// Set Headers request.setHeader( QNetworkRequest::ContentTypeHeader,"application/json" ); request.setHeader( QNetworkRequest::ContentLengthHeader,QString(postDataSize).toUtf8() ); // Post and connect mReply = mAccessManager->post(request, byteArrayObject); connect(mReply, SIGNAL(finished()), this, SLOT(onFinished()));

 



Follow me on twitter @RileyGB - https://twitter.com/RileyGB
View my BB10 OpenSource projects - https://github.com/RileyGB/BlackBerry10-Samples
Please use plain text.
Developer
RileyGB
Posts: 282
Registered: ‎08-06-2010
My Device: Z10

Re: Sending data via HTTP post

Correction. The line

 

    QByteArray postDataSize = QByteArray::number(body.size());

 

 Should be:

  QByteArray postDataSize = QByteArray::number(byteArrayObject.size());

 



Follow me on twitter @RileyGB - https://twitter.com/RileyGB
View my BB10 OpenSource projects - https://github.com/RileyGB/BlackBerry10-Samples
Please use plain text.
New Developer
shashi1990
Posts: 24
Registered: ‎04-04-2013
My Device: developer
My Carrier: non blackberry user

Re: Sending data via HTTP post

[ Edited ]

where did you specify service name which you want to use on server

Please use plain text.