我正在使用libcurl将图像上传到bing视觉搜索api(v7)并将json响应存储在string中。 但是我没有得到字符串的完整响应。 请检查以下详细信息
POST请求如下
CURLcode res;
struct curl_httppost *post = NULL;
struct curl_httppost *last = NULL;
struct curl_slist* m_pHeaders {nullptr};
curl_global_init(CURL_GLOBAL_ALL);
string strResponse;
cURl = curl_easy_init();
if(cURl)
{
curl_easy_setopt(cURl, CURLOPT_URL,"https://api.cognitive.microsoft.com/bing/v7.0/images/visualsearch?mkt=en-us&safeSearch=Moderate");
curl_easy_setopt(cURl, CURLOPT_POST, 1);
curl_easy_setopt(cURl, CURLOPT_VERBOSE, 1L);
curl_slist_append(m_pHeaders,"Accept: application/json");
m_pHeaders = curl_slist_append(m_pHeaders,"Content-Type: multipart/form-data");
m_pHeaders = curl_slist_append(m_pHeaders,"Ocp-Apim-Subscription-Key:MYKEYHERE");
curl_easy_setopt(cURl, CURLOPT_HTTPHEADER, m_pHeaders);
/* upload to this place */
curl_formadd(&post, &last, CURLFORM_COPYNAME, "image",
CURLFORM_FILE, "/mypathtoUSBDrive/sunny.jpg",
CURLFORM_CONTENTTYPE, "image/*", CURLFORM_END);
curl_easy_setopt(cURl, CURLOPT_HTTPPOST, post);
curl_easy_setopt(cURl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(cURl, CURLOPT_TIMEOUT, 60L);
curl_easy_setopt(cURl, CURLOPT_WRITEFUNCTION, m_WriteBufferCallback);
curl_easy_setopt(cURl, CURLOPT_WRITEDATA, this);
/* tell it to "upload" to the URL */
//curl_easy_setopt(m_curl, CURLOPT_UPLOAD, 1L);
res = curl_easy_perform(cURl);
/* Check for errors */
if(res != CURLE_OK)
{
printf("curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
}
else
{
printf("RESULT is : %s\n",strResponse.c_str());
}
curl_slist_free_all(m_pHeaders);
curl_formfree(post);
curl_easy_cleanup(cURl);
}
Writecallback如下
size_t m_WriteBufferCallback(char *ptr, size_t size,size_t nmemb, void *stream)
{
printf("SIZE Is (%d) and nMEMB is (%d)",size,nmemb);
for (size_t c = 0; c < size * nmemb; c++)
{
strResponse.push_back(ptr[c]);
}
return size * nmemb;
}
响应如下
RESULT is : {"_type": "ImageKnowledge", "instrumentation": {"_type": "ResponseInstrumentation"}, "tags": [{"displayName": "", "actions": [{"_type": "ImageModuleAction", "actionType": "PagesIncluding", "data": {"value": [{"webSearchUrl": "https:\/\/www.bing.com\/images\/search?view=detailv2&FORM=OIIRPO&id=6F32937A127F30DE909228C3E3B2C63349DEF1A4&simid=608017422531691677", "name": "Actress - Bollywood Wallpapers Download FREE (Page - 821 ...", "thumbnailUrl": "https:\/\/tse4.mm.bing.net\/th?id=OIP._5HDL12anYZseYCZ3L9EMwHaEK&pid=Api", "datePublished": "2019-10-06T22:04:00.0000000Z", "isFamilyFriendly": true, "contentUrl": "https:\/\/mrpopat.in\/admin\/upload\/wallpaper\/2015010314202751881532910028.jpg", "hostPageUrl": "https:\/\/mrpopat.in\/popat_mobile\/wallpaper_collection.php?page=820&&&pid=143&cid=149", "contentSize": "139790 B", "encodingFormat": "jpeg", "hostPageDisplayUrl": "https:\/\/mrpopat.in\/popat_mobi
Writecallback打印如下 **
SIZE Is (1) and nMEMB is (8192)
SIZE Is (1) and nMEMB is (8192)
SIZE Is (1) and nMEMB is (8192)
SIZE Is (1) and nMEMB is (8192)
SIZE Is (1) and nMEMB is (8192)
SIZE Is (1) and nMEMB is (8192)
SIZE Is (1) and nMEMB is (8192)
SIZE Is (1) and nMEMB is (8192)
SIZE Is (1) and nMEMB is (8192)
SIZE Is (1) and nMEMB is (8192)
SIZE Is (1) and nMEMB is (8192)
SIZE Is (1) and nMEMB is (8192)
SIZE Is (1) and nMEMB is (8192)
SIZE Is (1) and nMEMB is (8192)
SIZE Is (1) and nMEMB is (8192)
SIZE Is (1) and nMEMB is (8192)
SIZE Is (1) and nMEMB is (8192)
SIZE Is (1) and nMEMB is (8192)
SIZE Is (1) and nMEMB is (912)
**
请帮助您了解为什么无法在字符串中获得完整的响应,可能是怎么回事?