我正在尝试通过REST API guide(元数据+媒体)将文件上传到Google云端硬盘 但是收到响应错误:“不支持媒体类型'multipart / related; boundary = foo_bar_baz'。有效的媒体类型:[ / ]“
RESTResponseDataSetAdapter1->AutoUpdate = false;
RESTRequest1->Params->Clear();
RESTRequest1->ClearBody();
RESTRequest1->Resource = L"";
RESTRequest1->Method = rmPOST;
RESTClient1->BaseURL = L"https://www.googleapis.com/upload/drive/v3";
RESTRequest1->Resource = L"/files?uploadType=multipart";
RESTClient1->ContentType = L"multipart/related; boundary=foo_bar_baz";
UnicodeString Boundary = L"foo_bar_baz";
UnicodeString Body = L"";
RESTRequest1->Params->AddItem();
RESTRequest1->Params->Items[0]->Kind = pkHTTPHEADER;
RESTRequest1->Params->Items[0]->name = L"Content-Type";
RESTRequest1->Params->Items[0]->Value = L"multipart/related; boundary=" + Boundary;
// RESTRequest1->Params->Items[0]->Value = "application/json; charset=UTF-8";
// RESTRequest1->Params->AddItem();
// RESTRequest1->Params->Items[1]->Kind = pkHTTPHEADER;
// RESTRequest1->Params->Items[1]->name = L"Content-Length";
Body = (UnicodeString)sLineBreak + L"--" + Boundary + sLineBreak;
Body = Body + L"Content-Type: application/json; charset=UTF-8" + sLineBreak + sLineBreak;
Body = Body + L"{\"name\":\"test.json\"}" + sLineBreak + sLineBreak;
Body = Body + L"--" + Boundary + sLineBreak;
Body = Body + L"Content-Type: application/json" + sLineBreak + sLineBreak;
Body = Body + L"{\"test\":\"test\"}" + sLineBreak;
Body = Body + L"--" + Boundary + "--";
// RESTRequest1->Params->Items[1]->Value = IntToStr((__int64)Body.Length()*sizeof(Body));
RESTRequest1->AddBody(Body, TRESTContentType::ctMULTIPART_RELATED);
答案 0 :(得分:0)
解决方案:RESTRequest->Params->Items[0]->Options = TRESTRequestParameterOptions() << TRESTRequestParameterOption::poDoNotEncode;