qtCUrl How to add (--data) option?



  • 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



  • @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



  • @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.



  • @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.


  • 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!!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.