[英]Delphi REST API Post Sample
Can someone post a simple example of a JSON POST request to an API using Delphi 2005. I have found numerous examples using GET but the API provider does not allow requests via HTTP GET and does not support URL encoding parameters. 有人可以使用Delphi 2005向API发布一个简单的JSON POST请求示例。我发现了许多使用GET的示例,但API提供程序不允许通过HTTP GET发出请求,并且不支持URL编码参数。
I am brand new to calling REST services (have used SOAP in the past) so please let me know if you require more information. 我是调用REST服务的新手(过去使用过SOAP),如果您需要更多信息,请告诉我。
One option, using some part of our mORMot Open Source framework: 一种选择,使用我们的mORMot开源框架的某些部分:
uses SynCrtSock, SynCommons;
var t: variant;
begin
TDocVariant.New(t);
t.name := 'john';
t.year := 1982;
TWinHTTP.Post('http://servername/resourcename',t,'Content-Type: application/json');
end;
Note that here you can construct your JSON content using a custom variant
storage , which will be converted as JSON text when sent to the server. 请注意,您可以在此处使用自定义
variant
存储构建JSON内容,该存储在发送到服务器时将转换为JSON文本。
You would just use Indy's TIdHTTP
component and call the Post
method. 您只需使用Indy的
TIdHTTP
组件并调用Post
方法。 Pass the URL as the first argument and your JSON string as the second argument. 将URL作为第一个参数传递,将JSON字符串作为第二个参数传递。 Something like this:
像这样的东西:
procedure TForm1.Button1Click(Sender: TObject);
var
jsonToSend: TStringList;
http: TIdHTTP;
begin
http := TIdHTTP.Create(nil);
try
http.HandleRedirects := True;
http.ReadTimeout := 5000;
jsonToSend := TStringList.create;
try
jsonToSend.Add('{ Your JSON-encoded request goes here }');
http.Post('http://your.restapi.url', jsonToSend);
finally
jsonToSend.Destroy;
end;
finally
http.Destroy;
end;
end;
I'm assuming you are already able to encode and decode the JSON and that you were just asking how to perform an HTTP post using Delphi. 我假设您已经能够对JSON进行编码和解码,而您只是在询问如何使用Delphi执行HTTP帖子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.