Delphi REST API Mail Sample

Can anyone post a simple JSON POST request example for an API using Delphi 2005. I have found many examples using GET, but the API provider does not allow requests via HTTP GET and does not support URL parameters.

I am new to REST service calls (previously used SOAP), so please let me know if you need more information.

+7
source share
2 answers

You simply use the Indy TIdHTTP component and call the Post method. Pass the URL as the first argument and the JSON string as the second argument. More or less 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 assume that you can already encode and decode JSON, and that you were just asking how to do HTTP publishing using Delphi.

+7
source

One option using some part of our open source mORMot platform:

 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; 

Please note that here you can create your JSON content using custom variant storage , which will be converted to JSON text when sent to the server.

+7
source

All Articles