如何使用libcurl定义元数据?

时间:2019-05-10 10:06:30

标签: c++ http

我使用 <ItemGroup> <Reference Include="DevExpress.Charts.v18.2.Core, Version=18.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> <HintPath>..\dll_references\DevExpress.Charts.v18.2.Core.dll</HintPath> </Reference> </ItemGroup> (命令行工具)执行以下命令,将文件上传到Google驱动器:

curl

以下带有curl --request POST \ 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart' \ --header 'Authorization: Bearer "..."' \ -F "metadata={name : 'backup.zip'};type=application/json;charset=UTF-8" \ -F "@backup.zip;type=application/zip" 的程序只能使用libcurlURL并使用formdata上载文件:

header

如何在#include <stdio.h> #include <curl/curl.h> int main(void) { CURL *curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if(curl) { struct curl_slist *chunk= NULL; chunk= curl_slist_append(chunk, "Authorization: Bearer \"...\""); curl_slist_append(chunk, "Accept: application/json"); curl_slist_append(chunk, "Content-Type: application/json"); curl_mimepart *part= NULL; curl_mime_type(part, "application/octet-stream"); struct curl_httppost* formpost = NULL; struct curl_httppost* lastptr = NULL; curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "sendfile", CURLFORM_FILE, "main.c", CURLFORM_END); /* Perform the request, res will get the return code */ //curl_easy_setopt(curl, CURLOPT_URL, "https://www.googleapis.com/drive/v3/files"); curl_easy_setopt(curl, CURLOPT_URL, "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); curl_easy_setopt(curl, CURLOPT_MIMEPOST, part); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); curl_easy_perform(curl); /* always cleanup */ curl_easy_cleanup(curl); } curl_global_cleanup(); return 0; } 中指定-F "metadata={name : 'backup.zip'};type=application/json;charset=UTF-8"

我用libcurl尝试过,但这没用。

1 个答案:

答案 0 :(得分:0)

您正在将HTTP请求的顶级Content-Type标头设置为application/json,这是错误的。根据Google的文档Send a multipart upload request,它必须是multipart/related

您显示的命令行将类型application/json的MIME部分插入到POST表单数据中,但是您在代码中没有执行相同的操作。

此外,您不应同时使用CURLOPT_MIMEPOSTCURLOPT_HTTPPOST。使用一个或另一个,而不是两者。 CURLOPT_HTTPPOST已弃用CURLOPT_MIMEPOST,并被CURLOPT_MIMEPOST 替换。而且,您甚至都没有将正确的输入值传递给curl_mime*。它期望curl_mimepart*,而不是#include <stdio.h> #include <curl/curl.h> int main(void) { CURL *curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if (curl) { /* set custom HTTP headers */ struct curl_slist *header = NULL; // uncomment this if you are not using libcurl 7.61.0 or later... // // header = curl_slist_append(header, "Authorization: Bearer \"...\""); // header = curl_slist_append(header, "Content-Type: multipart/related"); header = curl_slist_append(header, "Accept: application/json"); /* set MIME post content */ curl_mime *mime = curl_mime_init(curl); curl_mimepart *part = curl_mime_addpart(mime); curl_mime_name(part, "metadata"); curl_mime_type(part, "application/json;charset=UTF-8"); curl_mime_data(part, "{\"name\" : \"backup.zip\"}", CURL_ZERO_TERMINATED); part = curl_mime_addpart(mime); curl_mime_type(part, "application/zip"); curl_mime_filedata(part, "backup.zip"); /* Perform the request, res will get the return code */ curl_easy_setopt(curl, CURLOPT_URL, "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"); curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header); curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime); // comment this out if you are not using libcurl 7.61.0 or later... // curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); curl_easy_setopt(curl, CURLOPT_XOAUTH2_BEARER, "..."); // res = curl_easy_perform(curl); /* always cleanup */ curl_slist_free_all(header); curl_mime_free(mime); curl_easy_cleanup(curl); } curl_global_cleanup(); return 0; }

请尝试以下类似操作:

Content-Type

或者也许是这样(我不确定使用MIME正文时指定HTTP #include <stdio.h> #include <curl/curl.h> int main(void) { CURL *curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if (curl) { /* set custom HTTP headers */ struct curl_slist *header = NULL; // uncomment this if you are not using libcurl 7.61.0 or later... // // header = curl_slist_append(header, "Authorization: Bearer \"...\""); // header = curl_slist_append(header, "Accept: application/json"); /* set MIME post content */ curl_mime *mime = curl_mime_init(curl); curl_mime *rel = curl_mime_init(curl); curl_mimepart *part = curl_mime_addpart(rel); curl_mime_name(part, "metadata"); curl_mime_type(part, "application/json;charset=UTF-8"); curl_mime_data(part, "{\"name\" : \"backup.zip\"}", CURL_ZERO_TERMINATED); part = curl_mime_addpart(rel); curl_mime_type(part, "application/zip"); curl_mime_filedata(part, "backup.zip"); part = curl_mime_addpart(mime); curl_mime_type(part, "multipart/related"); curl_mime_subparts(part, rel); /* Perform the request, res will get the return code */ curl_easy_setopt(curl, CURLOPT_URL, "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"); curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header); curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime); // comment this out if you are not using libcurl 7.61.0 or later... // curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); curl_easy_setopt(curl, CURLOPT_XOAUTH2_BEARER, "..."); // res = curl_easy_perform(curl); /* always cleanup */ curl_slist_free_all(header); curl_mime_free(mime); curl_easy_cleanup(curl); } curl_global_cleanup(); return 0; } 的正确方法-关于此主题的文档和示例非常有限,我发现的大多数示例都是针对SMTP的,不是HTTP):


    <ion-slides [options]="slideOpts" (ionSlideDidChange)="slideDidChange()" #myslides style="height:100%; width:100%;">
        <ion-slide>
            <app-noticed-events style="height:100%; width:100%;"></app-noticed-events>
        </ion-slide>
        <ion-slide>
            <app-home style="height:100%; width:100%;"></app-home>
        </ion-slide>
        <ion-slide>
          <app-map style="height:100%; width:100%;"></app-map>
        </ion-slide>
      </ion-slides>