Unsolved Send multipart/form-data json to webserver using cUrl
-
I have trouble with posting json data to server. I can't get json data from server.
Result isHTTP/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); }
-
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 ?
-
result is
"{"error":true,"code":"badip","message":"Unrecognized IP address (121.138.65.213) or bad authenticati"...
-
-
@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 ContinueHTTP/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"}]
-
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); }
-
@darongyi I think https://github.com/KoalaBR/spotlight/blob/ef90127ec6508b7822e57301e73c0ec7ca452333/reverseimagesearch.cpp
this is solution. modify code again. -
@darongyi can't connect curl_mime osx
-
@darongyi If any idea, leave comment.
I decide to use qnetworkaccessmanager.
Thank u all.