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 create HTTP request using form-data in QT
Forum Updated to NodeBB v4.3 + New Features

How to create HTTP request using form-data in QT

Scheduled Pinned Locked Moved Unsolved General and Desktop
apinetwork sockethttp-postnetworkauthform
10 Posts 4 Posters 4.9k 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.
  • H Offline
    H Offline
    HemantSuryawanshi
    wrote on last edited by
    #1

    I am trying to sent an HTTP request to server with key-value pair form-data in it. but I think I missing something while sending request that's why I didn't receive the correct response from server and I am not able to get whats missing in it.

    I have an API which generates terrain data and create its file. Here is my curl request

    curl --location --request POST 'http://abc.xyz.com:123/generate' \
    --header 'Content-Type;' \
    --header 'Authorization: Bearer xxxxxxxx-xxxxxxx-xxxxxxx-xxx-xxx-xx-xxxxx' \
    --form 'lat="19.3"' \
    --form 'long="73.20"' \
    --form 'radius="5"'
    

    when I hit the request from postman it gives correct output but when I hit api from code it gives me "\n" as response.

    Here is my code for same in QT

    void RestApiHelper::generateTerrainData(float lat, float lon, qint32 radius)
    {
        QUrl targateUrl = m_apiUrlHelper->getGenerateTerrainURL();
        qDebug() << targateUrl;
        QNetworkRequest request;
        request.setUrl(targateUrl);
    
        QString data = "xxxxx-xxxxxxx-xxxxxxxxx-xxxxxxxx";
        QString headerData = m_apiUrlHelper->getApiAuthorizationType() + data;
        request.setRawHeader( "Authorization", headerData.toLocal8Bit());
        qDebug() << "headerData :" << headerData.toLocal8Bit();
    
        QByteArray payload;
        payload.append("lat", lat);
        payload.append("long", lon);
        payload.append("radius", radius);
    
        QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
        QHttpPart textPart;
        //textPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("form-data"));
        textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""));
        textPart.setBody(payload);
        multiPart->append(textPart);
    
        QNetworkAccessManager *restclient;
        restclient = new QNetworkAccessManager();
        //restclient->post(request,multiPart);
        QNetworkReply *reply = restclient->post(request, multiPart);
        multiPart->setParent(reply);
    
        disconnect(&m_generateTerrainProcessor, SIGNAL(dataReady(QString)), this, nullptr);
        connect(restclient, SIGNAL(finished(QNetworkReply*)), &m_generateTerrainProcessor, SLOT(handleAPIResponse(QNetworkReply*)));
        connect(&m_generateTerrainProcessor, SIGNAL(dataReady(QString)), this, SLOT(handleDataReady(QString)));
    }
    

    Here I sent data in kay/value pair to QByteArray and set this body to multiport. I tried with this way but didnt get expected result. The expected result is link of a generated file.

    can anyone please tell me what I am doing wrong or is there any other method to assign kay-value pair form-data to the request in QT.

    raven-worxR 1 Reply Last reply
    0
    • H HemantSuryawanshi

      I am trying to sent an HTTP request to server with key-value pair form-data in it. but I think I missing something while sending request that's why I didn't receive the correct response from server and I am not able to get whats missing in it.

      I have an API which generates terrain data and create its file. Here is my curl request

      curl --location --request POST 'http://abc.xyz.com:123/generate' \
      --header 'Content-Type;' \
      --header 'Authorization: Bearer xxxxxxxx-xxxxxxx-xxxxxxx-xxx-xxx-xx-xxxxx' \
      --form 'lat="19.3"' \
      --form 'long="73.20"' \
      --form 'radius="5"'
      

      when I hit the request from postman it gives correct output but when I hit api from code it gives me "\n" as response.

      Here is my code for same in QT

      void RestApiHelper::generateTerrainData(float lat, float lon, qint32 radius)
      {
          QUrl targateUrl = m_apiUrlHelper->getGenerateTerrainURL();
          qDebug() << targateUrl;
          QNetworkRequest request;
          request.setUrl(targateUrl);
      
          QString data = "xxxxx-xxxxxxx-xxxxxxxxx-xxxxxxxx";
          QString headerData = m_apiUrlHelper->getApiAuthorizationType() + data;
          request.setRawHeader( "Authorization", headerData.toLocal8Bit());
          qDebug() << "headerData :" << headerData.toLocal8Bit();
      
          QByteArray payload;
          payload.append("lat", lat);
          payload.append("long", lon);
          payload.append("radius", radius);
      
          QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
          QHttpPart textPart;
          //textPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("form-data"));
          textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""));
          textPart.setBody(payload);
          multiPart->append(textPart);
      
          QNetworkAccessManager *restclient;
          restclient = new QNetworkAccessManager();
          //restclient->post(request,multiPart);
          QNetworkReply *reply = restclient->post(request, multiPart);
          multiPart->setParent(reply);
      
          disconnect(&m_generateTerrainProcessor, SIGNAL(dataReady(QString)), this, nullptr);
          connect(restclient, SIGNAL(finished(QNetworkReply*)), &m_generateTerrainProcessor, SLOT(handleAPIResponse(QNetworkReply*)));
          connect(&m_generateTerrainProcessor, SIGNAL(dataReady(QString)), this, SLOT(handleDataReady(QString)));
      }
      

      Here I sent data in kay/value pair to QByteArray and set this body to multiport. I tried with this way but didnt get expected result. The expected result is link of a generated file.

      can anyone please tell me what I am doing wrong or is there any other method to assign kay-value pair form-data to the request in QT.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @HemantSuryawanshi said in How to create HTTP request using form-data in QT:

      QByteArray payload;
      payload.append("lat", lat);
      payload.append("long", lon);
      payload.append("radius", radius);

      this does not create a key/value pair!

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #3
            auto json_obj = createJsonObject( key, double_data );
            QJsonDocument json_document;
            json_document.setObject( json_obj );
            post( json_document.toJson() );
        

        auto MyClass::createJsonObject( const QString & key, const double value ) ->QJsonObject
        {
        QJsonObject final_json_obj;
        final_json_obj.insert( key, QJsonValue( value ) );
        ...........
        }

        raven-worxR 1 Reply Last reply
        0
        • JoeCFDJ JoeCFD
              auto json_obj = createJsonObject( key, double_data );
              QJsonDocument json_document;
              json_document.setObject( json_obj );
              post( json_document.toJson() );
          

          auto MyClass::createJsonObject( const QString & key, const double value ) ->QJsonObject
          {
          QJsonObject final_json_obj;
          final_json_obj.insert( key, QJsonValue( value ) );
          ...........
          }

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @JoeCFD
          how is this related?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          JoeCFDJ 1 Reply Last reply
          1
          • raven-worxR raven-worx

            @JoeCFD
            how is this related?

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #5

            @raven-worx Use json to create key/value pairs and then convert it to QByteArray. That is how I do HTTP request.

            raven-worxR 1 Reply Last reply
            0
            • JoeCFDJ JoeCFD

              @raven-worx Use json to create key/value pairs and then convert it to QByteArray. That is how I do HTTP request.

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @JoeCFD
              thats how you do http requests on most REST interfaces, yes.
              form encoded data is plain key value pairs, not json.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              JoeCFDJ 1 Reply Last reply
              3
              • raven-worxR raven-worx

                @JoeCFD
                thats how you do http requests on most REST interfaces, yes.
                form encoded data is plain key value pairs, not json.

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #7

                @raven-worx Sorry. I did not read his code properly.

                Does this linlk help?
                https://forum.qt.io/topic/56708/solved-qnetworkaccessmanager-adding-a-multipart-form-data-to-a-post-request/6

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  HemantSuryawanshi
                  wrote on last edited by
                  #8

                  @raven-worx @JoeCFD thanks for your replies. I also tried with json before but get same output.
                  @JoeCFD I also referred the link you stated before. its for sending file. but I don't want to send a file, I just want to send key/value pair data using form-data.
                  can you please tell me how can I do this in Qt.

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    Bonnie
                    wrote on last edited by
                    #9

                    If your form-data is just plain text you should be able to do that by setting ContentTypeHeader to "application/x-www-form-urlencoded" and then just post a query string, instead of using QHttpMultiPart. Something like

                    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
                    restclient->post(request, "lat=19.3&long=73.20&radius=5");
                    
                    1 Reply Last reply
                    3
                    • H Offline
                      H Offline
                      HemantSuryawanshi
                      wrote on last edited by
                      #10

                      @Bonnie it worked. Thanks for your response

                      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