Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting JSON Response from an REST POST - application/json

Getting JSON Response from an REST POST - application/json

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Grove
    wrote on last edited by
    #1

    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

    mrjjM 1 Reply Last reply
    0
    • G 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,
      Grove

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      G 1 Reply Last reply
      0
      • mrjjM mrjj

        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

        G Offline
        G Offline
        Grove
        wrote on last edited by Grove
        #3

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

        mrjjM 1 Reply Last reply
        0
        • G Grove

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            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.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              saeidparand
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                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.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  Grove
                  wrote on last edited by
                  #8

                  @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"
                  
                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    Grove
                    wrote on last edited by
                    #9

                    by adding:

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

                    And executing:

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

                    I now have some results:

                    "\x1F\xEF\xBF\xBD\b"
                    

                    But how to deal with this?

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      Grove
                      wrote on last edited by
                      #10

                      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?

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved