Einzelnen Beitrag anzeigen

mjustin

Registriert seit: 14. Apr 2008
3.005 Beiträge
 
Delphi 2009 Professional
 
#6

AW: XLM Payload als Post

  Alt 10. Aug 2016, 13:49
Es darf keine TStringList verwendet werden. Indy benutzt dann für den Request das Format application/x-www-form-urlencoded.

Stattdessen muss ein Stream verwendet werden, zum Beispiel ein TStringStream.

Ein Beispiel für ein HTTPS POST mit einem JSON Body habe ich hier:

https://mikejustin.wordpress.com/201...-6-https-post/

Delphi-Quellcode:
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('{"日本語":42}',
        TEncoding.UTF8);
      try
        HTTP.Request.Accept := 'application/json';
        HTTP.Request.ContentType := 'application/json';
        ResponseBody := HTTP.Post('https://httpbin.org/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;
  ReportMemoryLeaksOnShutdown := True;
end.
Michael Justin
  Mit Zitat antworten Zitat