Not getting any response from post request in form url encoded format
-
Hi All,
I am trying to do a post request to a webservice with body containing user id , password and target info.
When i tried using PostMan , I am getting proper response, but when tried using Qt code,I am not getting response.
Below are the snap for the postman an Qt code. Could you please help. (For safety reason I am not posting real user id and password)Thanks
Nitin -
Hi,
QNetworkAccessManager works asynchronously. You need to use signals and slots to properly handle the request and its answer,
-
Hi SGaist,
Do I need to do like this:
void HttpWorker::onPostAnswer(QNetworkReply* reply)
{
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(statusCode == 200)
{
const QJsonDocument response = QJsonDocument::fromJson(reply->readAll());
const QJsonObject response_json = QJsonDocument::fromVariant(response.object().value("smsession").toVariant()).object();
}
}void HttpWorker::GetSiteminderToken()
{
QNetworkReply* reply;
QNetworkRequest request(QUrl("https://xxtest.employees.www.uprr.com/admin/login.fcc"));
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");QUrlQuery postArray; postArray.addQueryItem("TARGET" , "https://xxtest.employees.www.uprr.com/hmp/alert-handler/1.0/secure/jas/fetch-session-cookie"); postArray.addQueryItem("USER" , "abcd1234"); postArray.addQueryItem("PASSWORD" , "rfte123");
QNetworkAccessManager manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply)),
SLOT(onPostAnswer(QNetworkReply*)));
reply = manager->post(request, postArray.toString(QUrl::FullyEncoded).toUtf8());}
But Still status code is 0. It should be 200.
Thanks
-
Please use coding tags around your code to make it readable (the </> button on the toolbar).
That said the connect statement looks wrong since you have once a pointer for the signal and the other an object for the slot.
-
void HttpWorker::postFinished(QNetworkReply* rep)
{
QByteArray bts = rep->readAll();
QString str = QString::fromUtf8(bts);
qDebug().noquote() << "Reply:"<<str;
rep->deleteLater();
}QString HttpWorker::GetSiteminderToken()
{QNetworkReply* reply; QNetworkRequest request(QUrl("https://xxtest.employees.www.uprr.com/admin/login.fcc")); request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded"); QUrlQuery postArray; postArray.addQueryItem("TARGET" , "https://xxtest.emp->oyees.www.uprr.com/hmp/alert-handler/1.0/secure/jas/fetch-session-cookie"); postArray.addQueryItem("USER" , "abcd1234"); postArray.addQueryItem("PASSWORD" , "frtg4567"); QNetworkAccessManager *manager = new QNetworkAccessManager(this); connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(postFinished(QNetworkReply*))); manager->post(request, postArray.toString(QUrl::FullyEncoded).toUtf8());
}
Still getting str as "" .
Thanks
-
This may be helpful. Try to check your return in postFinished(). m_reply = m_networkAccessManager.post( request, byte_array ); if ( nullptr != m_reply ) { if ( m_reply->isRunning() ) { connect( m_reply, &QNetworkReply::finished, this, &MyClass::postFinished ); } else { /* may be finished immediately*/ postFinished(); } }
-
@ndiwan
Put a breakpoint on entry toonPostAnswer()
. Use the watch window to have a dig around in*reply
to see what you can see there! Anything interesting? You say returned state code is 0, which I don't think is a code HTTP would return at all. I did also suggest you verify you do not get any error. -
Try your QUrl with the following format:
http://user:password@domain.com/