Einzelnen Beitrag anzeigen

Edelfix

Registriert seit: 6. Feb 2015
Ort: Stadtoldendorf
216 Beiträge
 
Delphi 10.4 Sydney
 
#38

AW: rest server wie muss das post Command aussehen oder welchen Server könnte ich ver

  Alt 22. Okt 2019, 14:59
Noch Detaillierter:

Delphi-Quellcode:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Net.URLClient,
  System.Net.HttpClient, System.Net.HttpClientComponent;

type
  TForm1 = class(TForm)
    Button1: TButton;
    NetHTTPClient1: TNetHTTPClient;
    procedure Button1Click(Sender: TObject);
  private
    sErr: string;
    slParams: TStringList;
    slHeaders: TStringList;
    Function doGetData(sBody, BodyMode, url: String; var sRP: String): Boolean;
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  sBody: string;
  url: string;
  sRP: string;
  bodyMode: string;
begin
  slParams:= TStringList.Create;
  slHeaders:= TStringList.Create;
  try
    sBody := '{"Hasta":"Lavista Baby "}';
    bodyMode := 'raw';
    url:= 'deine Ziel URL';
    if doGetData(sBody,bodyMode,url,sRP) then
      showmessage(sRP);
  finally
    slParams.Free;
    slHeaders.Free;
  end;
end;

function TForm1.doGetData(sBody, BodyMode, url: String; var sRP: String): Boolean;
var
  uri: TUri;
  srRQBody: TStringStream;
  RP: IHTTPResponse;
  sRQContent: String;
  sRPContent: String;
  srRPBody: TStringStream;
  I: Integer;
  StatusCode: Integer;
  StatusText: String;
  ResponseText: String;
begin
  Result := false;
  srRQBody:= TStringStream.Create;
  srRPBody:= TStringStream.Create;
  try
    //-- URL
    try
      uri := TUri.Create(URL);
    except on E: Exception do
      sErr:= E.Message;
    end;

    //-- Params
    for I := 0 to slParams.Count-1 do
      uri.AddParameter(slParams.Names[i], slParams.ValueFromIndex[i]);

    //-- Auth
      //SetAuth(RQRec);

    //-- Headers
    for I := 0 to slHeaders.Count-1 do
      NetHTTPClient1.CustomHeaders[slHeaders.Names[i]] := slHeaders.ValueFromIndex[i];

    //-- Body
    sRQContent := sBody;
    if BodyMode='rawthen
    begin
      srRQBody.WriteString(sRQContent);
      srRQBody.Position := 0;
    end else
    if BodyMode='x-www-form-urlencodedthen
    begin
      sRQContent := 'data'+'='+sRQContent;
      srRQBody.WriteString(sRQContent);
      srRQBody.Position := 0;
    end;


    RP := NetHTTPClient1.Post(uri.ToString, srRQBody, srRPBody);
    sRPContent := srRPBody.DataString;
    StatusCode := RP.StatusCode;
    StatusText := RP.StatusText;
    ResponseText := sRPContent;
    if StatusCode=200 then
      Result := true;
  finally
    srRQBody.Free;
    srRPBody.Free;
  end;
end;

end.
  Mit Zitat antworten Zitat