[英]How to post data with a ContentType of 'multipart/form-data' in Delphi REST?
I am trying to send a request to a REST API using multipart/form-data
as the content type.我正在尝试使用
multipart/form-data
作为内容类型向 REST API 发送请求。
I always get "HTTP/1.1 500 Internal Error" as a response.我总是收到“HTTP/1.1 500 内部错误”作为响应。
I tried sending requests to methods that require application/x-www-form-urlencoded
and had success, though.不过,我尝试向需要
application/x-www-form-urlencoded
方法发送请求并取得了成功。
How can I achieve getting a success response from my API using multipart/form-data
?如何使用
multipart/form-data
从我的 API 获得成功响应?
Here is my code:这是我的代码:
procedure TForm10.Button1Click(Sender: TObject);
var
RESTClient1: TRESTClient;
RESTRequest1: TRESTRequest;
strImageJSON : string;
Input: TIdMultipartFormDataStream;
begin
Input := TIdMultipartFormDataStream.Create;
Input.Clear;
Input.AddFormField('Email', 'tugba.xx@allianz.com.tr');
Input.AddFormField('Password', 'xxxx');
RESTClient1 := TRESTClient.Create('http://192.168.1.172:81/');
RESTRequest1 := TRESTRequest.Create(nil);
RESTRequest1.Method := TRESTRequestMethod.rmPOST;
RESTRequest1.Resource := 'api/Mobile/MobileLoginControl';
RESTRequest1.AddBody(Input,TRESTContentType.ctMULTIPART_FORM_DATA);
RESTRequest1.Client := RESTClient1;
RESTRequest1.Execute;
strImageJSON := RESTRequest1.Response.Content;
end;
Embarcadero's REST component has its own built in multipart/form-data
capabilities via the TRESTRequest.AddParameter()
method: Embarcadero 的 REST 组件通过
TRESTRequest.AddParameter()
方法拥有自己的内置multipart/form-data
功能:
procedure TForm10.Button1Click(Sender: TObject);
var
RESTClient1: TRESTClient;
RESTRequest1: TRESTRequest;
strImageJSON : string;
begin
RESTClient1 := TRESTClient.Create('http://192.168.1.172:81/');
try
RESTRequest1 := TRESTRequest.Create(nil);
try
RESTRequest1.Method := TRESTRequestMethod.rmPOST;
RESTRequest1.Resource := 'api/Mobile/MobileLoginControl';
RESTRequest1.AddParameter('Email', 'tugba.xx@allianz.com.tr', TRESTRequestParameterKind.pkREQUESTBODY);
RESTRequest1.AddParameter('Password', 'xxxx', TRESTRequestParameterKind.pkREQUESTBODY);
RESTRequest1.Client := RESTClient1;
RESTRequest1.Execute;
strImageJSON := RESTRequest1.Response.Content;
finally
RESTRequest1.Free;
end;
finally
RESTClient1.Free;
end;
end;
You don't need to use use Indy's TIdMultiPartFormDataStream
, especially when you are not using Indy's TIdHTTP
.您不需要使用 Indy 的
TIdMultiPartFormDataStream
,尤其是当您不使用 Indy 的TIdHTTP
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.