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. Send multipart/form-data json to webserver using cUrl
Forum Updated to NodeBB v4.3 + New Features

Send multipart/form-data json to webserver using cUrl

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 2.0k Views 2 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.
  • D Offline
    D Offline
    darongyi
    wrote on 1 Jan 2019, 14:30 last edited by darongyi 1 Feb 2019, 10:27
    #1

    I have trouble with posting json data to server. I can't get json data from server.
    Result is

    HTTP/1.1 403 Forbidden
    Date: Tue, 01 Jan 2019 14:19:36 GMT
    Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips mod_wsgi/3.4 Python/2.7.5 mod_perl/2.0.10 Perl/v5.16.3
     Content-Length: 110
     Content-Type: text/json; charset=utf-8
    

    This is my original code

    I change code using cUrl.

    
    void UpCurl::postHttpMultiPart()
    {
        int res = 0;
    
        //---------- Curl
        CURLcode hResult;
        m_post = nullptr;
        m_last = nullptr;
    
        // Handle to the curl
        m_hCurl = curl_easy_init();
    
        // set up the header
        curl_formadd(&m_post,
            &m_last,
            CURLFORM_COPYNAME, "cache-control:",
            CURLFORM_COPYCONTENTS, "no-cache",
            CURLFORM_END);
    
        curl_formadd(&m_post,
            &m_last,
            CURLFORM_COPYNAME, "content-type:",
            CURLFORM_COPYCONTENTS, "multipart/form-data",
            CURLFORM_END);
        curl_easy_setopt(m_hCurl, CURLOPT_URL, qPrintable(MAIN_SERVER));
        curl_easy_setopt(m_hCurl, CURLOPT_VERBOSE, 1L);
    
        struct curl_slist *chunk = nullptr;
    
    
        QString m_token = authentication();
        QString header = QString("auth: %1").arg(m_token);
        if(header.size() > 0)
           chunk = curl_slist_append(chunk, qPrintable(header));
        QString contentType = "content-type: json";
        chunk = curl_slist_append(chunk, qPrintable(contentType));
    
    
        //curl_easy_setopt(m_hCurl, CURLOPT_HTTPHEADER, chunk);
        QByteArray postArray = postFields;
    
       //curl_easy_setopt(m_hCurl, CURLOPT_WRITEHEADER, write_data);
        curl_easy_setopt(m_hCurl, CURLOPT_WRITEFUNCTION,write_data);
        curl_easy_setopt(m_hCurl, CURLOPT_WRITEDATA, response);
        if(postFields.size() > 0)
        {   curl_easy_setopt(m_hCurl, CURLOPT_POST, 1L);
            curl_easy_setopt(m_hCurl, CURLOPT_POSTFIELDS,postArray.constData());
    
    
        }
        hResult = curl_easy_perform(m_hCurl);
        long code;
        curl_easy_getinfo(m_hCurl, CURLINFO_RESPONSE_CODE, &code);
        res = (hResult == CURLE_OK) ? code : hResult;
        curl_easy_cleanup(m_hCurl);
        curl_slist_free_all(chunk);
    }
    
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 1 Jan 2019, 15:44 last edited by mrjj 1 Jan 2019, 15:45
      #2

      Hi
      You seem to get
      HTTP/1.1 403 Forbidden
      Does your app have the correct permission to perform the action ?

      So i wonder if that is the reason, it is not working ?

      https://airbrake.io/blog/http-errors/403-forbidden-error

      1 Reply Last reply
      2
      • D Offline
        D Offline
        darongyi
        wrote on 2 Jan 2019, 05:48 last edited by
        #3

        result is

        "{"error":true,"code":"badip","message":"Unrecognized IP address (121.138.65.213) or bad authenticati"...
        
        J 1 Reply Last reply 2 Jan 2019, 05:55
        0
        • D darongyi
          2 Jan 2019, 05:48

          result is

          "{"error":true,"code":"badip","message":"Unrecognized IP address (121.138.65.213) or bad authenticati"...
          
          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 2 Jan 2019, 05:55 last edited by
          #4

          @darongyi Check whether your app has access rights or the IP is correct as @mrjj suggested.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 1 Reply Last reply 2 Jan 2019, 05:59
          1
          • J jsulm
            2 Jan 2019, 05:55

            @darongyi Check whether your app has access rights or the IP is correct as @mrjj suggested.

            D Offline
            D Offline
            darongyi
            wrote on 2 Jan 2019, 05:59 last edited by
            #5

            @jsulm
            I access terminal .. result is like that.
            yoshimi~$ perl -e ' print ""' | curl https://login.cridb.com/api/cridb -i -F json='{"command":"search","record_type":"thing","query":"tigra"}' -F 'auth=<-'
            HTTP/1.1 100 Continue

            HTTP/1.1 200 OK
            Date: Wed, 02 Jan 2019 05:58:06 GMT
            Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips mod_wsgi/3.4 Python/2.7.5 mod_perl/2.0.10 Perl/v5.16.3
            Content-Length: 326
            Content-Type: text/json; charset=utf-8

            [{"subtype":"person","status":"online","name":"Tigra","type":"thing","id":"89792"},{"subtype":"person","status":"offline","name":"Tigran Vartanovich Petrosian","type":"thing","id":"35410"},{"subtype":"person","status":"offline","name":"Dikran II) Tigranes II (Tigranes the Great; also Tigran II","type":"thing","id":"44923"}]

            1 Reply Last reply
            0
            • D Offline
              D Offline
              darongyi
              wrote on 2 Jan 2019, 07:25 last edited by darongyi 1 Feb 2019, 07:52
              #6

              Change code and get result this

              <!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head><title>400 Bad Request</title></head><body><h1>Bad Request</h1><p>Your browser sent a request that this server could not understand.<br /></p></body></html>
              
              size_t UpCurl::write_data(void *buffer, size_t size, size_t nmemb, 
              void *userdata)
              {
              
                  UNUSED(userdata);
              
                  stringstream strmResponse;
                  size_t nReal = size * nmemb;
                  strmResponse << string((char*)buffer, size*nmemb);
                  string sLine("");
              
                  QByteArray temp;
                  while (getline(strmResponse, sLine)) {
                      // Surcharge += : si jamais la réponse fait plusieurs lignes
                      temp += sLine.c_str();
                  }
              
                  qDebug() << temp;
                  return nReal;
              }
              

              code

              void UpCurl::startUpload()
              {
              
                  CURLcode hResult;
              
                  m_post = nullptr;
                  m_last = nullptr;
              
               
                  #ifdef WINDOWS
                      curl_global_init(CURL_GLOBAL_WIN32);
                  #endif
                  #ifdef LINUX
                      curl_global_init(CURL_GLOBAL_ALL);
                  #endif
              
                  // Handle to the curl
                  m_hCurl = curl_easy_init();
              
                  struct curl_slist *chunk = nullptr;
              
                  // Specify the API Endpoint
                  hResult = curl_easy_setopt(m_hCurl, CURLOPT_URL, qPrintable(m_curlUrl.c_str()));
              
                  QString m_token = authentication();
                  QString header = QString("auth: %1").arg(m_token);
                  std::string h_auth = header.toStdString();
              
              
                  if(header.size() > 0)
                     chunk = curl_slist_append(chunk, h_auth.c_str());
              
                  chunk = curl_slist_append(chunk, "content-type: multipart/form-data");
                  curl_easy_setopt(m_hCurl, CURLOPT_HTTPHEADER, chunk);
              //    QString contentType = "Content-Type: application/json";
              //    chunk = curl_slist_append(chunk, qPrintable(contentType));
              
                  std::string response;
                  string jsonData = "json=" + postFields.toStdString();
                  curl_formadd(&m_post, &m_last, CURLFORM_COPYNAME, "json",
                                 CURLFORM_COPYCONTENTS, jsonData.c_str(), CURLFORM_END);
              
                  // Recovering the server response
                  curl_easy_setopt(m_hCurl, CURLOPT_WRITEFUNCTION, write_data);
                  curl_easy_setopt(m_hCurl, CURLOPT_WRITEDATA, &response);
                  // User-Agent
                  curl_easy_setopt(m_hCurl, CURLOPT_USERAGENT, USER_AGENT);
              
                  // Specify the HTTP Method
                  curl_easy_setopt(m_hCurl, CURLOPT_NOPROGRESS, 1);
                  curl_easy_setopt(m_hCurl, CURLOPT_POSTFIELDS, jsonData.c_str());
                  curl_easy_setopt(m_hCurl, CURLOPT_POST, 1L);
                  hResult = curl_easy_perform(m_hCurl);
              
                  long code;
                  int res = 0;
                  curl_easy_getinfo(m_hCurl, CURLINFO_RESPONSE_CODE, &code);
                  res = (hResult == CURLE_OK) ? code : hResult;
                  curl_easy_cleanup(m_hCurl);
                  curl_global_cleanup();
              
                  finishProcedure(hResult);
              }
              
              
              D 1 Reply Last reply 2 Jan 2019, 08:14
              0
              • D darongyi
                2 Jan 2019, 07:25

                Change code and get result this

                <!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head><title>400 Bad Request</title></head><body><h1>Bad Request</h1><p>Your browser sent a request that this server could not understand.<br /></p></body></html>
                
                size_t UpCurl::write_data(void *buffer, size_t size, size_t nmemb, 
                void *userdata)
                {
                
                    UNUSED(userdata);
                
                    stringstream strmResponse;
                    size_t nReal = size * nmemb;
                    strmResponse << string((char*)buffer, size*nmemb);
                    string sLine("");
                
                    QByteArray temp;
                    while (getline(strmResponse, sLine)) {
                        // Surcharge += : si jamais la réponse fait plusieurs lignes
                        temp += sLine.c_str();
                    }
                
                    qDebug() << temp;
                    return nReal;
                }
                

                code

                void UpCurl::startUpload()
                {
                
                    CURLcode hResult;
                
                    m_post = nullptr;
                    m_last = nullptr;
                
                 
                    #ifdef WINDOWS
                        curl_global_init(CURL_GLOBAL_WIN32);
                    #endif
                    #ifdef LINUX
                        curl_global_init(CURL_GLOBAL_ALL);
                    #endif
                
                    // Handle to the curl
                    m_hCurl = curl_easy_init();
                
                    struct curl_slist *chunk = nullptr;
                
                    // Specify the API Endpoint
                    hResult = curl_easy_setopt(m_hCurl, CURLOPT_URL, qPrintable(m_curlUrl.c_str()));
                
                    QString m_token = authentication();
                    QString header = QString("auth: %1").arg(m_token);
                    std::string h_auth = header.toStdString();
                
                
                    if(header.size() > 0)
                       chunk = curl_slist_append(chunk, h_auth.c_str());
                
                    chunk = curl_slist_append(chunk, "content-type: multipart/form-data");
                    curl_easy_setopt(m_hCurl, CURLOPT_HTTPHEADER, chunk);
                //    QString contentType = "Content-Type: application/json";
                //    chunk = curl_slist_append(chunk, qPrintable(contentType));
                
                    std::string response;
                    string jsonData = "json=" + postFields.toStdString();
                    curl_formadd(&m_post, &m_last, CURLFORM_COPYNAME, "json",
                                   CURLFORM_COPYCONTENTS, jsonData.c_str(), CURLFORM_END);
                
                    // Recovering the server response
                    curl_easy_setopt(m_hCurl, CURLOPT_WRITEFUNCTION, write_data);
                    curl_easy_setopt(m_hCurl, CURLOPT_WRITEDATA, &response);
                    // User-Agent
                    curl_easy_setopt(m_hCurl, CURLOPT_USERAGENT, USER_AGENT);
                
                    // Specify the HTTP Method
                    curl_easy_setopt(m_hCurl, CURLOPT_NOPROGRESS, 1);
                    curl_easy_setopt(m_hCurl, CURLOPT_POSTFIELDS, jsonData.c_str());
                    curl_easy_setopt(m_hCurl, CURLOPT_POST, 1L);
                    hResult = curl_easy_perform(m_hCurl);
                
                    long code;
                    int res = 0;
                    curl_easy_getinfo(m_hCurl, CURLINFO_RESPONSE_CODE, &code);
                    res = (hResult == CURLE_OK) ? code : hResult;
                    curl_easy_cleanup(m_hCurl);
                    curl_global_cleanup();
                
                    finishProcedure(hResult);
                }
                
                
                D Offline
                D Offline
                darongyi
                wrote on 2 Jan 2019, 08:14 last edited by
                #7

                @darongyi I think https://github.com/KoalaBR/spotlight/blob/ef90127ec6508b7822e57301e73c0ec7ca452333/reverseimagesearch.cpp
                this is solution. modify code again.

                D 1 Reply Last reply 2 Jan 2019, 09:48
                1
                • D darongyi
                  2 Jan 2019, 08:14

                  @darongyi I think https://github.com/KoalaBR/spotlight/blob/ef90127ec6508b7822e57301e73c0ec7ca452333/reverseimagesearch.cpp
                  this is solution. modify code again.

                  D Offline
                  D Offline
                  darongyi
                  wrote on 2 Jan 2019, 09:48 last edited by
                  #8

                  @darongyi can't connect curl_mime osx

                  D 1 Reply Last reply 2 Jan 2019, 15:06
                  0
                  • D darongyi
                    2 Jan 2019, 09:48

                    @darongyi can't connect curl_mime osx

                    D Offline
                    D Offline
                    darongyi
                    wrote on 2 Jan 2019, 15:06 last edited by
                    #9

                    @darongyi If any idea, leave comment.
                    I decide to use qnetworkaccessmanager.
                    Thank u all.

                    1 Reply Last reply
                    0

                    1/9

                    1 Jan 2019, 14:30

                    • Login

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