Delphi REST API 发布示例 [英] Delphi REST API Post Sample
问题描述
有人可以使用 Delphi 2005 向 API 发布 JSON POST 请求的简单示例.我发现了许多使用 GET 的示例,但 API 提供程序不允许通过 HTTP GET 进行请求并且不支持 URL 编码参数.
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.
我是调用 REST 服务的新手(过去曾使用过 SOAP),所以如果您需要更多信息,请告诉我.
I am brand new to calling REST services (have used SOAP in the past) so please let me know if you require more information.
推荐答案
您只需使用 Indy 的 TIdHTTP
组件并调用 Post
方法.将 URL 作为第一个参数传递,将您的 JSON 字符串作为第二个参数传递.像这样:
You would just use Indy's TIdHTTP
component and call the Post
method. Pass the URL as the first argument and your JSON string as the second argument. 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;
我假设您已经能够对 JSON 进行编码和解码,并且您只是在询问如何使用 Delphi 执行 HTTP 发布.
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.
这篇关于Delphi REST API 发布示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!