我正在嘗試POST使用TCustomRESTRequest, 添加我嘗試訪問的 API 所需的更具體的標頭。
當我向我的請求添加標頭時,我嘗試添加指定的Content-Type: multipart/form-data:
var
RESTRequest : TCustomRESTRequest;
RESTClient : TRESTClient;
Response : TCustomRESTResponse;
begin
RESTRequest := TCustomRESTRequest.create(nil);
RESTClient := TRESTClient.create('');
try
RESTClient.BaseURL := 'urltoreach';
RESTRequest.Client := RESTClient;
RESTRequest.AddParameter('Content-Type','multipart/form-data',pkHTTPHEADER,poDoNotEncode);
RESTRequest.Method := rmPOST;
RESTRequest.Execute;
Response := RESTRequest.Response;
if Response.Status.Success then
begin
showmessage('success');
end;
else
begin
showmessage(Response.StatusText ' : ' Response.Content);
end;
finally
RESTRequest.free;
RESTClient.free;
end;
end;
我遇到的問題是我收到一條錯誤訊息:
'不支持的媒體型別:{"errors":"不支持的媒體型別 multipart/form-data"}'
這意味著/即使我請求poDoNotEncode作為引數選項,我也會被編碼。
你知道為什么嗎?
uj5u.com熱心網友回復:
好吧,對于初學者來說,你應該使用TRESTRequest而不是TCustomRESTRequest.
而且,不要TRESTRequest.AddParameter(pkHTTPHEADER)用于設定 HTTPContent-Type標頭。 TRESTClient/將根據您發送的正文內容的型別TRESTRequest自行確定要使用的內容。Content-Type
但是,在您的示例中,您實際上根本沒有發布任何正文內容。請參閱如何在 Delphi REST 中發布 ContentType 為“multipart/form-data”的資料?和RESTRequest 如何在 HTTP Post 正文中發送 multipartformdata?
您需要添加一些正文引數。 TRESTClient/TRESTRequest將multipart/form-data在滿足以下條件之一時使用:
有一個引數 using
ContentType = ctMULTIPART_FORM_DATA(但不確定如何設定它)或者,有超過 1 個引數使用
Kind = pkREQUESTBODY或者,使用 1 個引數
Kind = pkREQUESTBODY,使用 1 個或多個引數Kind = pkGETorPOST
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/509896.html
標籤:德尔福
