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. qtCUrl How to add (--data) option?

qtCUrl How to add (--data) option?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.9k 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.
  • GuapoG Offline
    GuapoG Offline
    Guapo
    wrote on last edited by
    #1

    Hi,

    I need to make this command:

    curl --request POST
    --url https://pos-api.ifood.com.br/v1.0/events/acknowledgment
    --header 'Authorization: bearer {acess_token}'
    --header 'Cache-Control: no-cache'
    --header 'Content-Type: application/json'
    --data '[{"id":"Event_id1"},{"id":"Event_id2"},{"id":"Event_id3"},{"id":"Event_id4"}]'

    I don´t know how I can add this option using QtCUrl.

            QJsonDocument Lista;
    
            QtCUrl cUrl;
            QUrl url("https://pos-api.ifood.com.br/v1.0/events/acknowledgment");
            QtCUrl::Options opt;
            opt[CURLOPT_URL] = url;
            opt[CURLOPT_POST] = true;
            opt[CURLOPT_FOLLOWLOCATION] = true;
            opt[CURLOPT_FAILONERROR] = true;
            QStringList headers;
            headers
                << "cache-control: no-cache"
                << "Content-Type: application/json"
                << QString("Authorization: bearer %1").arg(token);
            opt[CURLOPT_HTTPHEADER] = headers;
            
           // here this is my problem!!!!
            QString Data=QString(Lista.toJson());
            opt[CURLOPT_HEADERDATA]=Data;
    
            QString val1 = cUrl.exec(opt);
    
    

    Can you help me?

    Thanks in advance

    1 Reply Last reply
    0
    • GuapoG Offline
      GuapoG Offline
      Guapo
      wrote on last edited by
      #5

      Yes @Pablo-J-Rogina , these brackets aren´t necessary for one id...I tried with and without brackets.

      Now I´m using another function ( without QtCUrl ).

              CURL *curl;
              CURLcode res;
              QString autorizacao;
      
              autorizacao=QString("Authorization: bearer %1").arg(token);
      
                curl_global_init(CURL_GLOBAL_ALL);
      
                std::string jsonstr = qPrintable(str);
      
                /* get a curl handle */
                curl = curl_easy_init();
                if(curl) {
                  /* First set the URL that is about to receive our POST. This URL can
                     just as well be a https:// URL if that is what should receive the
                     data. */
                    struct curl_slist *headers = NULL;
                    headers = curl_slist_append(headers, qPrintable(autorizacao));
                    headers = curl_slist_append(headers, "Cache-Control: no-cache");
                    headers = curl_slist_append(headers, "Content-Type: application/json");
      
                    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonstr.c_str());
                    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,jsonstr.size());
                    curl_easy_setopt(curl, CURLOPT_URL, "https://pos-api.ifood.com.br/v1.0/events/acknowledgment");
                    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
                  /* Now specify the POST data */
      
                  /* Perform the request, res will get the return code */
                  res = curl_easy_perform(curl);
                  /* Check for errors */
                  if(res != CURLE_OK)
                    fprintf(stderr, "curl_easy_perform() failed: %s\n",
                            curl_easy_strerror(res));
      
                  /* always cleanup */
                  curl_easy_cleanup(curl);
                }
                curl_global_cleanup();
                return 0;
      
      

      This code fixed my problem.

      I don´t know wireshark....I´ll install here to debug my first function.

      After posting yesterday, I noticed the bullshit I wrote about the double quotes... : )

      Thank you very much!!!

      1 Reply Last reply
      0
      • Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #2

        @Guapo being QtCUrl just a wrapper to libcurl, you may want to look for examples regarding such library. I think this answer may apply to you, as it's using CURLOPT_POSTFIELDS

        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
        • GuapoG Offline
          GuapoG Offline
          Guapo
          wrote on last edited by
          #3

          @Pablo-J-Rogina you are all right!

          This option CURLOPT_POSTFIELDS is fine, but I have another problem to use it.

          This is a json data to send to server: "[ { "id": "0572c06f-4e48-4a63-8a62-277f8f15c091" }"

          The cUrl doesn´t accept this character "\" before quotes. I´m also using at command line and there is the same error "The requested URL returned error: 400 Bad Request"

                  QtCUrl cUrl;
                  QUrl url("https://pos-api.ifood.com.br/v1.0/events/acknowledgment");
                  QtCUrl::Options opt;
                  opt[CURLOPT_URL] = url;
                  opt[CURLOPT_POST] = true;
                  opt[CURLOPT_FOLLOWLOCATION] = true;
                  opt[CURLOPT_FAILONERROR] = true;
                  QStringList headers;
                  headers
                      << QString("Authorization: bearer %1").arg(token)
                      << "Cache-control: no-cache"
                      << "Content-Type: application/json";
                  opt[CURLOPT_HTTPHEADER] = headers;
          
                  QString Data=QString(Lista.toJson()).trimmed().simplified();
          
           // this is a example of my json data
                 Data="[ { \"id\": \"0572c06f-4e48-4a63-8a62-277f8f15c091\" }";
                 opt[CURLOPT_POSTFIELDS]=Data;
          
                  QString val1 = cUrl.exec(opt);
          
                  if (cUrl.lastError().isOk()) {
          
                      bool ok;
                      // json is a QString containing the JSON data
                      QtJson::JsonObject result = QtJson::parse(val1, ok).toMap();
          
                      return true;
                  }
                  else {
                      qDebug() << url;
                      qDebug() << QString("Error: %1\nBuffer: %2").arg(cUrl.lastError().text()).arg(cUrl.errorBuffer());
          
                      return false;
                  }
          
          

          @Pablo-J-Rogina thank you for your help.

          I don´t know how can I fix this.

          1 Reply Last reply
          0
          • Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #4

            @Guapo

              1. your "Data" has a non required "[" (square bracket) at the beginning:
            { "id": "0572c06f-4e48-4a63-8a62-277f8f15c091" }
            
              1. Did you debug your code? Stopping just after value assignment to Data you'll see the " (double quotes) are Ok, there's no issue with slashes
              1. "400 Bad Request" is precisely that, the request you're sending is not understood by the server. You may want to capture network traffic (Wireshark is your friend) to see exactly what you are sending, both with command line tool or with Qt app.

            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
            1
            • GuapoG Offline
              GuapoG Offline
              Guapo
              wrote on last edited by
              #5

              Yes @Pablo-J-Rogina , these brackets aren´t necessary for one id...I tried with and without brackets.

              Now I´m using another function ( without QtCUrl ).

                      CURL *curl;
                      CURLcode res;
                      QString autorizacao;
              
                      autorizacao=QString("Authorization: bearer %1").arg(token);
              
                        curl_global_init(CURL_GLOBAL_ALL);
              
                        std::string jsonstr = qPrintable(str);
              
                        /* get a curl handle */
                        curl = curl_easy_init();
                        if(curl) {
                          /* First set the URL that is about to receive our POST. This URL can
                             just as well be a https:// URL if that is what should receive the
                             data. */
                            struct curl_slist *headers = NULL;
                            headers = curl_slist_append(headers, qPrintable(autorizacao));
                            headers = curl_slist_append(headers, "Cache-Control: no-cache");
                            headers = curl_slist_append(headers, "Content-Type: application/json");
              
                            curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonstr.c_str());
                            curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,jsonstr.size());
                            curl_easy_setopt(curl, CURLOPT_URL, "https://pos-api.ifood.com.br/v1.0/events/acknowledgment");
                            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
                          /* Now specify the POST data */
              
                          /* Perform the request, res will get the return code */
                          res = curl_easy_perform(curl);
                          /* Check for errors */
                          if(res != CURLE_OK)
                            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                                    curl_easy_strerror(res));
              
                          /* always cleanup */
                          curl_easy_cleanup(curl);
                        }
                        curl_global_cleanup();
                        return 0;
              
              

              This code fixed my problem.

              I don´t know wireshark....I´ll install here to debug my first function.

              After posting yesterday, I noticed the bullshit I wrote about the double quotes... : )

              Thank you very much!!!

              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