Delphi: Send JSON to API

program JSONPostExample; {$APPTYPE CONSOLE} uses IdHTTP, IdGlobal, SysUtils, Classes; var HTTP: TIdHTTP; RequestBody: TStream; ResponseBody: string; begin HTTP := TIdHTTP.Create; try try RequestBody := TStringStream.Create('{"code":42}', TEncoding.UTF8); try HTTP.Request.Accept := 'application/json'; HTTP.Request.ContentType := 'application/json'; ResponseBody := HTTP.Post('', RequestBody); WriteLn(ResponseBody); WriteLn(HTTP.ResponseText); finally RequestBody.Free; end; except on E: EIdHTTPProtocolException do begin WriteLn(E.Message); WriteLn(E.ErrorMessage); end; on E: Exception do begin WriteLn(E.Message); end; end; finally HTTP.Free; end; ReadLn; end.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.