使用 curl & C++ 为 Dropbox 文件创建共享链接



我想使用curl&Windows10桌面上的C++。

我已经使用curl&C++

当我尝试用命令行创建链接时,它与一起工作

curl -X POST https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings --header "Authorization: Bearer <TOKEN>" --header "Content-Type: application/json" --data "{"path":"path_of_the_file"}"  

但是当我在C++中使用此代码执行相同操作时,它挂起<HTTP/1.1 100继续

这是我的代码:

CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
string readBuffer;
printf("Running curl test get shared link.n");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);              //no ssl
struct curl_slist *headers = NULL; // init to NULL is important 
headers = curl_slist_append(headers, "Authorization: Bearer <TOKEN>");
headers = curl_slist_append(headers, "Content-Type: ");
headers = curl_slist_append(headers, "Dropbox-API-Arg: {"path":"path_of_file"}");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POST, true);
curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
curl_easy_setopt(curl, CURLOPT_URL, "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings");
// 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: %sn",
curl_easy_strerror(res));
// always cleanup 
curl_easy_cleanup(curl);
cout << readBuffer << endl;
printf("nFinished curl test.n");
}
curl_global_cleanup();
printf("Done get shared link!n");

我尝试过使用内容类型:application/json和添加字段,但我无法重现我使用命令行所做的操作

代码中存在错误,导致未定义的行为。

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_easy_setopt(curl, CURLOPT_POST, true);
curl_easy_setopt(curl, CURLOPT_VERBOSE, true);

cUrl具有C API,C没有重载。因此,curl_easy_setopt是一个多参数函数,第三个参数类型取决于第二个参数值。CURLOPT_SSL_VERIFYPEERCURLOPT_VERBOSE需要长值,您可以将值作为int传递。第三个参数的大小与*printf函数中的大小非常重要。正确的呼叫必须是

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

第二。100 Continue表示您尚未传递POST请求的数据。服务器接收到请求并等待进一步的数据。看见libcurl示例-http post.c.

我无法重现我使用命令行所做的操作

--libcurl添加到命令行,以获得执行与命令行中相同操作的C代码。

谢谢S.M.这是的工作代码

CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
string readBuffer;
printf("Running curl test get shared link.n");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);             //no ssl
struct curl_slist *headers = NULL; // init to NULL is important 
headers = curl_slist_append(headers, "Authorization: Bearer <TOKEN>");
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{"path":"path_to_the_file"}");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings");
// 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: %sn",
curl_easy_strerror(res));
// always cleanup 
curl_easy_cleanup(curl);
cout << readBuffer << endl;
printf("nFinished curl test.n");
}
curl_global_cleanup();
printf("Done get shared link!n");

相关内容

  • 没有找到相关文章

最新更新