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. How to post list of QString to HTTP sever
Forum Updated to NodeBB v4.3 + New Features

How to post list of QString to HTTP sever

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 677 Views 1 Watching
  • 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.
  • zhmhZ Offline
    zhmhZ Offline
    zhmh
    wrote on last edited by
    #1

    I can post one data to server

    QNetworkRequest request(QUrl("http://site.com/api/PtData/"));   request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));
        QByteArray const data( "\"152,2019/05/14,08:38:21\"");
        QNetworkAccessManager nam;
        QNetworkReply *reply = nam.post(request, data);
    

    now I want to post a list of data from text file like:

    "102,2019/05/15,08:38:21" "153,2019/05/15,08:38:21" "154,2019/05/15,08:38:21" "155,2019/05/15,08:38:21" ......
    

    I wrote this code but in post() should be const qbytearray ,how can I fix this?

    QNetworkRequest request(QUrl("http://site.comr/api/PtData"));
       request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
       QFile file_obj(":/new/prefix1/2.txt");
       if(!file_obj.open(QIODevice::ReadOnly)){
           qDebug()<<"Failed to open ";
       }
    
       QTextStream file_text(&file_obj);
      QString  json_string =file_text.readAll();
       file_obj.close();
       QStringList values = json_string.split(" ");
       QNetworkAccessManager nam;
       for(int u=0;u<20;u++){
       QNetworkReply *reply =nam.post(request,  values.at(u));
    .
    .
    .
     }
    }
    
    Pablo J. RoginaP 1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      https://doc.qt.io/qt-5/qstring.html#toUtf8

      values.at(u).toUtf8()
      also:
      for(int u=0;u<values.count();u++)
      

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      3
      • zhmhZ zhmh

        I can post one data to server

        QNetworkRequest request(QUrl("http://site.com/api/PtData/"));   request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));
            QByteArray const data( "\"152,2019/05/14,08:38:21\"");
            QNetworkAccessManager nam;
            QNetworkReply *reply = nam.post(request, data);
        

        now I want to post a list of data from text file like:

        "102,2019/05/15,08:38:21" "153,2019/05/15,08:38:21" "154,2019/05/15,08:38:21" "155,2019/05/15,08:38:21" ......
        

        I wrote this code but in post() should be const qbytearray ,how can I fix this?

        QNetworkRequest request(QUrl("http://site.comr/api/PtData"));
           request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
           QFile file_obj(":/new/prefix1/2.txt");
           if(!file_obj.open(QIODevice::ReadOnly)){
               qDebug()<<"Failed to open ";
           }
        
           QTextStream file_text(&file_obj);
          QString  json_string =file_text.readAll();
           file_obj.close();
           QStringList values = json_string.split(" ");
           QNetworkAccessManager nam;
           for(int u=0;u<20;u++){
           QNetworkReply *reply =nam.post(request,  values.at(u));
        .
        .
        .
         }
        }
        
        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @zhmh said in How to post list of QString to HTTP sever:

        I want to post a list of data from text file like:

        Are you supposed to post a request per item or just only one request with the whole list?

        If the first is true, go with you have now (paying attention to @fcarney advice).

        If the latter is true, then you need to construct a JSON object (since you're stating "Content-Type" is JSON...) from the list and send that in the only request

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        zhmhZ 1 Reply Last reply
        6
        • Pablo J. RoginaP Pablo J. Rogina

          @zhmh said in How to post list of QString to HTTP sever:

          I want to post a list of data from text file like:

          Are you supposed to post a request per item or just only one request with the whole list?

          If the first is true, go with you have now (paying attention to @fcarney advice).

          If the latter is true, then you need to construct a JSON object (since you're stating "Content-Type" is JSON...) from the list and send that in the only request

          zhmhZ Offline
          zhmhZ Offline
          zhmh
          wrote on last edited by
          #4

          @Pablo-J.-Rogina how should I to construct a JSON object from the list?can you show an example for the latter?

          Pablo J. RoginaP 1 Reply Last reply
          0
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #5

            https://doc.qt.io/qt-5/qjsondocument.html#JsonFormat-enum

            QString json_string = "one,two,three,four,five,six";
            QByteArray joutput = QJsonDocument(QJsonArray::fromStringList(json_string.split(","))).toJson(QJsonDocument::Compact);
            

            Output:

            ["one","two","three","four","five","six"]
            

            Spend some time in the json docs. There are a few objects in there to be aware of.

            C++ is a perfectly valid school of magic.

            zhmhZ 1 Reply Last reply
            2
            • zhmhZ zhmh

              @Pablo-J.-Rogina how should I to construct a JSON object from the list?can you show an example for the latter?

              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #6

              @zhmh documentation is your friend...

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              3
              • fcarneyF fcarney

                https://doc.qt.io/qt-5/qjsondocument.html#JsonFormat-enum

                QString json_string = "one,two,three,four,five,six";
                QByteArray joutput = QJsonDocument(QJsonArray::fromStringList(json_string.split(","))).toJson(QJsonDocument::Compact);
                

                Output:

                ["one","two","three","four","five","six"]
                

                Spend some time in the json docs. There are a few objects in there to be aware of.

                zhmhZ Offline
                zhmhZ Offline
                zhmh
                wrote on last edited by
                #7

                @fcarney @Pablo-J-Rogina tnx

                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