Getting JSON Response from an REST POST - application/json
-
Hi,
I'm doing an REST POST call to an server with an application/json context.
The call is going alright but I can't get the response of it.
I already did try to get it on multiple different ways, but the I can't get the data.const int available = reply->bytesAvailable(); qDebug() << "bytesAvailable" << available; const QByteArray buffer = reply->readAll(); QByteArray response_data = reply->readAll(); qDebug() << response_data; QJsonDocument json = QJsonDocument::fromJson(response_data); QJsonObject obj = json.object(); qDebug()<<"view: " << obj["view"]; qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); QJsonDocument jsonDoc(QJsonDocument::fromJson(buffer)); QJsonObject jsonReply = jsonDoc.object(); QJsonObject respons1e = jsonReply["response"].toObject(); QJsonArray data = jsonReply["data"].toArray(); qDebug() << data;
The response of this is:
bytesAvailable 1024936 "" view: QJsonValue(null) 200 QJsonArray()
The bytes data is available because be bytes are available.
Is there a way to to get the data?Wkr,
Grove -
Hi,
I'm doing an REST POST call to an server with an application/json context.
The call is going alright but I can't get the response of it.
I already did try to get it on multiple different ways, but the I can't get the data.const int available = reply->bytesAvailable(); qDebug() << "bytesAvailable" << available; const QByteArray buffer = reply->readAll(); QByteArray response_data = reply->readAll(); qDebug() << response_data; QJsonDocument json = QJsonDocument::fromJson(response_data); QJsonObject obj = json.object(); qDebug()<<"view: " << obj["view"]; qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); QJsonDocument jsonDoc(QJsonDocument::fromJson(buffer)); QJsonObject jsonReply = jsonDoc.object(); QJsonObject respons1e = jsonReply["response"].toObject(); QJsonArray data = jsonReply["data"].toArray(); qDebug() << data;
The response of this is:
bytesAvailable 1024936 "" view: QJsonValue(null) 200 QJsonArray()
The bytes data is available because be bytes are available.
Is there a way to to get the data?Wkr,
GroveHi
But how does the data look ?
const QByteArray buffer = reply->readAll();
qDebug() << buffer;You call readAll() twice
const QByteArray buffer = reply->readAll();
QByteArray response_data = reply->readAll();
and it could be the reason that response_data is empty -
Hi
But how does the data look ?
const QByteArray buffer = reply->readAll();
qDebug() << buffer;You call readAll() twice
const QByteArray buffer = reply->readAll();
QByteArray response_data = reply->readAll();
and it could be the reason that response_data is empty -
Hi
did you check out
https://forum.qt.io/topic/61771/qnetworkaccessmanager-and-post-return/
You are not showing all of the code so its unclear if you used signals and slots. -
void Login::InsertApi(QString str)
{
QUrl serviceUrl = QUrl(str);
QNetworkRequest request(serviceUrl);
QJsonObject json; ;
json.insert("name", QString( "Amazing Pillow 92.10" ));
json.insert("price", QString( "199" ));
json.insert("description", QString( "The best pillow for amazing programmers." ));
json.insert("category_id", QString( "2" ));
json.insert("created",QString("2018-06-01 00:35:07"));
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
QNetworkAccessManager networkManager = new QNetworkAccessManager(this);
connect(networkManager,SIGNAL(finished(QNetworkReply)),this,SLOT(replyFinished(QNetworkReply*)));
networkManager->post(request,QJsonDocument(json).toJson());
qDebug() << "json:" << json;
}
/////////////
void Login::ReadAPI(QString api)
{
QNetworkRequest request(QUrl("http://192.168.1.104/ramona/employee/read.php"));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QNetworkAccessManager *m_nam=new QNetworkAccessManager();
QNetworkReply *reply = m_nam->get(request);
connect(reply, &QNetworkReply::finished, this, [this, reply] {
reply->deleteLater();
const QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
const QJsonArray array = doc.array();
for (const QJsonValue &value : array) {
//qDebug() << value.toObject();
qDebug() << "EMPID:" << value.toObject().find("empId")->toString();
qDebug() << "NCODE:" << value.toObject().find("ncode")->toString();
qDebug() << "EMPCODE:" << value.toObject().find("empcode")->toString();
qDebug() << "STATUES:" << value.toObject().find("statues")->toString();
}
});
} -
Hi
did you try move
reply->deleteLater(); in ReadAPI(QString api)
to the end of the function. Its fishy to ask to delete it first and then use it after. ( even if valid)
The code looks ok. -
@saeidparand I tried the QJSON part of your code but unfortunately this didn't solve a thing.
I have added some code to get the header. So now there is some more information to share:
QList<QByteArray> headerList = reply->rawHeaderList(); foreach(QByteArray head, headerList) { qDebug() << head << ":" << reply->rawHeader(head); }
returns:
"Cache-Control" : "no-cache,no-store" "Content-Type" : "text/plain" "Transfer-Encoding" : "chunked" "Content-Encoding" : "gzip" "Vary" : "Accept-Encoding" "Date" : "Sat, 24 Aug 2019 12:56:01 GMT"