Important: Please read the Qt Code of Conduct -

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)

    The bytes data is available because be bytes are available.
    Is there a way to to get the data?


  • Lifetime Qt Champion

    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

  • @mrjj in the debug data this is printed.
    It is an empty string.

  • Lifetime Qt Champion

    so the first readAll() for buffer also gives an empty string ?

  • Lifetime Qt Champion

    did you check out
    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"));
    QNetworkAccessManager networkManager = new QNetworkAccessManager(this);
    qDebug() << "json:" << json;
    void Login::ReadAPI(QString api)
    QNetworkRequest request(QUrl(""));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    QNetworkAccessManager *m_nam=new QNetworkAccessManager();
    QNetworkReply *reply = m_nam->get(request);
    connect(reply, &QNetworkReply::finished, this, [this, reply] {
    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();

  • Lifetime Qt Champion

    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);


    "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"

  • by adding:

    request.setRawHeader("Accept-Encoding", "gzip, deflate");

    And executing:

    QByteArray response = reply->readAll();
    qDebug() << QString(response).toUtf8();

    I now have some results:


    But how to deal with this?

  • I think I narrowed it down more...
    I have tried more POST and GET requests on the server without any problems.
    But when the response is gzip encoded and is chunked I can't get the JSON that it should return.

    Can anybody help me with this?

Log in to reply