2013-10-30 00:48:23 +01:00
|
|
|
unit MVCFramework.RESTClient;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes,
|
|
|
|
IdBaseComponent,
|
|
|
|
IdComponent,
|
|
|
|
IdTCPConnection,
|
|
|
|
IdTCPClient,
|
|
|
|
IdHTTP,
|
|
|
|
idURI,
|
|
|
|
DBXJSON,
|
|
|
|
IdMultipartFormData,
|
|
|
|
System.SysUtils;
|
|
|
|
|
|
|
|
type
|
|
|
|
TArrayOfString = array of string;
|
2014-03-24 17:37:08 +01:00
|
|
|
THttpCommand = (httpGET, httpPOST, httpPUT, httpDELETE, httpPATCH, httpTRACE);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
|
|
IRESTResponse = interface
|
|
|
|
function BodyAsString: string;
|
|
|
|
function BodyAsJsonObject: TJSONObject;
|
2013-11-09 14:22:11 +01:00
|
|
|
function BodyAsJsonValue: TJSONValue;
|
2013-10-30 00:48:23 +01:00
|
|
|
function ResponseCode: Word;
|
|
|
|
function ResponseText: string;
|
|
|
|
function Headers: TStringlist;
|
2013-11-05 14:57:50 +01:00
|
|
|
function GetContentType: String;
|
|
|
|
function GetContentEncoding: String;
|
2013-10-30 00:48:23 +01:00
|
|
|
function Body: TStringStream;
|
2014-03-31 11:25:16 +02:00
|
|
|
function GetHeaderValue(const Name: String): String;
|
2013-10-30 00:48:23 +01:00
|
|
|
procedure SetResponseCode(AResponseCode: Word);
|
|
|
|
procedure SetResponseText(AResponseText: string);
|
|
|
|
procedure SetHeaders(AHeaders: TStrings);
|
|
|
|
end;
|
|
|
|
|
|
|
|
TRESTResponse = class(TInterfacedObject, IRESTResponse)
|
|
|
|
private
|
2013-11-05 14:57:50 +01:00
|
|
|
FBody: TStringStream;
|
|
|
|
FResponseCode: Word;
|
|
|
|
FResponseText: string;
|
|
|
|
FHeaders: TStringlist;
|
2013-11-09 14:22:11 +01:00
|
|
|
FBodyAsJSONValue: TJSONValue;
|
2013-11-05 14:57:50 +01:00
|
|
|
FContentType: string;
|
|
|
|
FContentEncoding: String;
|
|
|
|
function GetHeader(const Value: String): String;
|
2013-10-30 00:48:23 +01:00
|
|
|
public
|
2014-03-31 11:25:16 +02:00
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
function BodyAsString: string;
|
|
|
|
function BodyAsJsonObject: TJSONObject;
|
2013-11-09 14:22:11 +01:00
|
|
|
function BodyAsJsonValue: TJSONValue;
|
2013-10-30 00:48:23 +01:00
|
|
|
function ResponseCode: Word;
|
|
|
|
function ResponseText: string;
|
|
|
|
function Headers: TStringlist;
|
|
|
|
function Body: TStringStream;
|
2013-11-05 14:57:50 +01:00
|
|
|
function GetContentType: String;
|
|
|
|
function GetContentEncoding: String;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
|
|
procedure SetResponseCode(AResponseCode: Word);
|
|
|
|
procedure SetResponseText(AResponseText: string);
|
|
|
|
procedure SetHeaders(AHeaders: TStrings);
|
|
|
|
constructor Create; virtual;
|
|
|
|
destructor Destroy; override;
|
2014-03-31 11:25:16 +02:00
|
|
|
function GetHeaderValue(const Name: string): string;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
TRESTClient = class(TInterfacedObject)
|
|
|
|
private
|
2013-11-05 14:57:50 +01:00
|
|
|
FServerName: string;
|
|
|
|
FServerPort: Word;
|
|
|
|
FBodyParams: TStringlist;
|
|
|
|
FQueryStringParams: TStringlist;
|
|
|
|
FAccept: string;
|
|
|
|
FRawBody: TStringStream;
|
|
|
|
FHTTP: TIdHTTP;
|
|
|
|
FContentType: string;
|
|
|
|
FLastSessionID: string;
|
2013-10-30 00:48:23 +01:00
|
|
|
FNextRequestIsAsynch: Boolean;
|
2013-11-09 14:22:11 +01:00
|
|
|
FAsynchProc: TProc<IRESTResponse>;
|
|
|
|
FAsynchProcErr: TProc<Exception>;
|
2013-11-05 14:57:50 +01:00
|
|
|
FPrimaryThread: TThread;
|
|
|
|
FMultiPartFormData: TIdMultiPartFormDataStream;
|
2013-11-09 14:22:11 +01:00
|
|
|
FAsynchProcAlways: TProc;
|
2013-10-30 00:48:23 +01:00
|
|
|
function EncodeQueryStringParams(const AQueryStringParams: TStrings;
|
|
|
|
IncludeQuestionMark: Boolean = true): string;
|
|
|
|
procedure SetBodyParams(const Value: TStringlist);
|
|
|
|
procedure SetQueryStringParams(const Value: TStringlist);
|
|
|
|
procedure SetAccept(const Value: string);
|
|
|
|
procedure SetContentType(const Value: string);
|
|
|
|
function GetSessionID: string;
|
|
|
|
procedure SetSessionID(const Value: string);
|
|
|
|
function GetBodyParams: TStringlist;
|
|
|
|
function GetQueryStringParams: TStringlist;
|
|
|
|
function GetRawBody: TStringStream;
|
|
|
|
procedure SetReadTimeout(const Value: Integer);
|
|
|
|
function GetReadTimeout: Integer;
|
2013-11-09 14:22:11 +01:00
|
|
|
procedure StartAsynchRequest(AHTTPMethod: THttpCommand; AUrl: string;
|
|
|
|
ABodyString: String); overload;
|
|
|
|
procedure StartAsynchRequest(AHTTPMethod: THttpCommand;
|
|
|
|
AUrl: string); overload;
|
2013-10-30 00:48:23 +01:00
|
|
|
procedure SetConnectionTimeout(const Value: Integer);
|
|
|
|
function GetConnectionTimeout: Integer;
|
|
|
|
procedure SetRequestHeaders(const Value: TStringlist);
|
|
|
|
|
|
|
|
strict protected
|
|
|
|
FRequestHeaders: TStringlist;
|
|
|
|
|
|
|
|
protected
|
|
|
|
procedure HandleCookies;
|
|
|
|
function EncodeResourceParams(AResourceParams: array of string): string;
|
|
|
|
function HttpCommandToString(const AHttpCommand: THttpCommand): string;
|
|
|
|
function SendHTTPCommand(const ACommand: THttpCommand;
|
|
|
|
const AAccept, AContentType, AUrl: string; ABodyParams: TStrings)
|
|
|
|
: IRESTResponse;
|
2013-11-09 14:22:11 +01:00
|
|
|
function SendHTTPCommandWithBody(const ACommand: THttpCommand;
|
|
|
|
const AAccept, AContentType, AUrl: string; ABodyString: String)
|
2013-10-30 00:48:23 +01:00
|
|
|
: IRESTResponse;
|
|
|
|
procedure HandleRequestCookies;
|
|
|
|
function GetMultipartFormData: TIdMultiPartFormDataStream;
|
|
|
|
|
|
|
|
public
|
|
|
|
constructor Create(const AServerName: string;
|
|
|
|
AServerPort: Word = 80); virtual;
|
|
|
|
destructor Destroy; override;
|
|
|
|
|
2013-11-09 14:22:11 +01:00
|
|
|
function AddFile(const FieldName, FileName: string;
|
|
|
|
const ContentType: string = ''): TRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
2013-11-09 14:22:11 +01:00
|
|
|
function Asynch(AProc: TProc<IRESTResponse>;
|
|
|
|
AProcErr: TProc<Exception> = nil; AProcAlways: TProc = nil): TRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
function ClearAllParams: TRESTClient;
|
|
|
|
function ResetSession: TRESTClient;
|
|
|
|
function Accept(const AcceptHeader: string): TRESTClient; overload;
|
|
|
|
function Accept: string; overload;
|
|
|
|
function ContentType(const ContentTypeHeader: string): TRESTClient;
|
|
|
|
overload;
|
|
|
|
function ContentType: string; overload;
|
|
|
|
// requests
|
|
|
|
function doGET(AResource: string; AResourceParams: array of string)
|
|
|
|
: IRESTResponse;
|
|
|
|
function doPOST(AResource: string; AResourceParams: array of string)
|
|
|
|
: IRESTResponse; overload;
|
|
|
|
function doPOST(AResource: string; AResourceParams: array of string;
|
|
|
|
AJSONValue: TJSONValue; AOwnsJSONBody: Boolean = true)
|
|
|
|
: IRESTResponse; overload;
|
2013-11-09 14:22:11 +01:00
|
|
|
function doPOST(AResource: string; AResourceParams: array of string;
|
|
|
|
ABodyString: String): IRESTResponse; overload;
|
2014-03-24 17:37:08 +01:00
|
|
|
function doPATCH(AResource: string; AResourceParams: array of string;
|
|
|
|
AJSONValue: TJSONValue; AOwnsJSONBody: Boolean = true)
|
|
|
|
: IRESTResponse; overload;
|
|
|
|
function doPATCH(AResource: string; AResourceParams: array of string;
|
|
|
|
ABodyString: String): IRESTResponse; overload;
|
2013-10-30 00:48:23 +01:00
|
|
|
function doPUT(AResource: string; AResourceParams: array of string)
|
|
|
|
: IRESTResponse; overload;
|
|
|
|
function doPUT(AResource: string; AResourceParams: array of string;
|
|
|
|
AJSONValue: TJSONValue; AOwnsJSONBody: Boolean = true)
|
|
|
|
: IRESTResponse; overload;
|
2013-11-11 12:23:49 +01:00
|
|
|
function doPUT(AResource: string; AResourceParams: array of string;
|
|
|
|
ABodyString: String): IRESTResponse; overload;
|
2013-10-30 00:48:23 +01:00
|
|
|
function doDELETE(AResource: string; AResourceParams: array of string)
|
|
|
|
: IRESTResponse;
|
|
|
|
property BodyParams: TStringlist read GetBodyParams write SetBodyParams;
|
|
|
|
property RawBody: TStringStream read GetRawBody;
|
|
|
|
property QueryStringParams: TStringlist read GetQueryStringParams
|
|
|
|
write SetQueryStringParams;
|
|
|
|
property SessionID: string read GetSessionID write SetSessionID;
|
|
|
|
property ReadTimeout: Integer read GetReadTimeout write SetReadTimeout;
|
|
|
|
property ConnectionTimeout: Integer read GetConnectionTimeout
|
|
|
|
write SetConnectionTimeout;
|
2013-11-09 14:22:11 +01:00
|
|
|
property RequestHeaders: TStringlist read FRequestHeaders
|
|
|
|
write SetRequestHeaders;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
function StringsToArrayOfString(const AStrings: TStrings): TArrayOfString;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{ TRSF }
|
|
|
|
|
|
|
|
function StringsToArrayOfString(const AStrings: TStrings): TArrayOfString;
|
|
|
|
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
SetLength(Result, AStrings.Count);
|
|
|
|
if AStrings.Count = 0 then
|
|
|
|
Exit;
|
|
|
|
for i := 0 to AStrings.Count - 1 do
|
|
|
|
Result[i] := AStrings[i];
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.Accept(const AcceptHeader: string): TRESTClient;
|
|
|
|
begin
|
|
|
|
SetAccept(AcceptHeader);
|
|
|
|
Result := Self;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.Accept: string;
|
|
|
|
begin
|
|
|
|
Result := FAccept;
|
|
|
|
end;
|
|
|
|
|
2013-11-09 14:22:11 +01:00
|
|
|
function TRESTClient.AddFile(const FieldName, FileName, ContentType: string)
|
|
|
|
: TRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
begin
|
|
|
|
GetMultipartFormData.AddFile(FieldName, FileName, ContentType);
|
|
|
|
end;
|
|
|
|
|
2013-11-09 14:22:11 +01:00
|
|
|
function TRESTClient.Asynch(AProc: TProc<IRESTResponse>;
|
|
|
|
AProcErr: TProc<Exception>; AProcAlways: TProc): TRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
begin
|
|
|
|
FNextRequestIsAsynch := true;
|
2013-11-09 14:22:11 +01:00
|
|
|
FAsynchProc := AProc;
|
|
|
|
FAsynchProcErr := AProcErr;
|
|
|
|
FAsynchProcAlways := AProcAlways;
|
2013-10-30 00:48:23 +01:00
|
|
|
Result := Self;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.ClearAllParams: TRESTClient;
|
|
|
|
begin
|
|
|
|
if Assigned(FRawBody) then
|
|
|
|
begin
|
|
|
|
FRawBody.Size := 0;
|
|
|
|
FRawBody.Position := 0;
|
|
|
|
end;
|
|
|
|
if Assigned(FBodyParams) then
|
|
|
|
FBodyParams.Clear;
|
|
|
|
if Assigned(FQueryStringParams) then
|
|
|
|
FQueryStringParams.Clear;
|
|
|
|
Result := Self;
|
|
|
|
FNextRequestIsAsynch := False;
|
2013-11-09 14:22:11 +01:00
|
|
|
FAsynchProc := nil;
|
|
|
|
FAsynchProcErr := nil;
|
|
|
|
FAsynchProcAlways := nil;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.ContentType: string;
|
|
|
|
begin
|
|
|
|
Result := FContentType;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.ContentType(const ContentTypeHeader: string): TRESTClient;
|
|
|
|
begin
|
|
|
|
SetContentType(ContentTypeHeader);
|
|
|
|
Result := Self;
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TRESTClient.Create(const AServerName: string; AServerPort: Word);
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
FPrimaryThread := TThread.CurrentThread;
|
|
|
|
FServerName := AServerName;
|
|
|
|
FServerPort := AServerPort;
|
|
|
|
FBodyParams := nil; // TStringlist.Create;
|
|
|
|
FQueryStringParams := nil; // TStringlist.Create;
|
|
|
|
FRawBody := nil; // TStringStream.Create('');
|
|
|
|
FAccept := 'application/json';
|
|
|
|
FContentType := 'application/json; charset=utf-8';
|
|
|
|
FRequestHeaders := TStringlist.Create;
|
|
|
|
|
|
|
|
FHTTP := TIdHTTP.Create(nil);
|
|
|
|
FHTTP.ReadTimeout := 20000;
|
|
|
|
// FHTTP.AllowCookies := true;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TRESTClient.Destroy;
|
|
|
|
begin
|
|
|
|
FreeAndNil(FBodyParams);
|
|
|
|
FreeAndNil(FQueryStringParams);
|
|
|
|
FreeAndNil(FRawBody);
|
|
|
|
FreeAndNil(FRequestHeaders);
|
|
|
|
FreeAndNil(FMultiPartFormData);
|
|
|
|
FHTTP.Free;
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.doDELETE(AResource: string;
|
|
|
|
AResourceParams: array of string): IRESTResponse;
|
|
|
|
var
|
|
|
|
url: string;
|
|
|
|
begin
|
|
|
|
url := 'http://' + FServerName + ':' + inttostr(FServerPort) + AResource +
|
|
|
|
EncodeResourceParams(AResourceParams) + EncodeQueryStringParams
|
|
|
|
(FQueryStringParams);
|
|
|
|
|
|
|
|
if FNextRequestIsAsynch then
|
|
|
|
begin
|
|
|
|
Result := nil;
|
|
|
|
StartAsynchRequest(httpDELETE, url);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Result := SendHTTPCommand(httpDELETE, FAccept, FContentType, url, nil);
|
|
|
|
ClearAllParams;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TRESTClient.StartAsynchRequest(AHTTPMethod: THttpCommand;
|
|
|
|
AUrl: string);
|
|
|
|
begin
|
2013-11-09 14:22:11 +01:00
|
|
|
StartAsynchRequest(AHTTPMethod, AUrl, '');
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
2013-11-09 14:22:11 +01:00
|
|
|
procedure TRESTClient.StartAsynchRequest(AHTTPMethod: THttpCommand;
|
|
|
|
AUrl: string; ABodyString: String);
|
2013-10-30 00:48:23 +01:00
|
|
|
var
|
|
|
|
th: TThread;
|
|
|
|
begin
|
|
|
|
th := TThread.CreateAnonymousThread(
|
|
|
|
procedure
|
|
|
|
var
|
|
|
|
R: IRESTResponse;
|
|
|
|
begin
|
|
|
|
try
|
2013-11-09 14:22:11 +01:00
|
|
|
R := SendHTTPCommandWithBody(AHTTPMethod, FAccept, FContentType, AUrl,
|
|
|
|
ABodyString);
|
2013-10-30 00:48:23 +01:00
|
|
|
TMonitor.Enter(TObject(R));
|
|
|
|
try
|
2013-11-09 14:22:11 +01:00
|
|
|
FAsynchProc(R);
|
2013-10-30 00:48:23 +01:00
|
|
|
ClearAllParams;
|
|
|
|
finally
|
|
|
|
TMonitor.Exit(TObject(R));
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
on E: Exception do
|
|
|
|
begin
|
2013-11-09 14:22:11 +01:00
|
|
|
FAsynchProcErr(E);
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
end;
|
2013-11-09 14:22:11 +01:00
|
|
|
if Assigned(FAsynchProcAlways) then
|
|
|
|
FAsynchProcAlways();
|
2013-10-30 00:48:23 +01:00
|
|
|
end);
|
|
|
|
th.Start;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.doGET(AResource: string; AResourceParams: array of string)
|
|
|
|
: IRESTResponse;
|
|
|
|
var
|
|
|
|
url: string;
|
|
|
|
begin
|
|
|
|
url := 'http://' + FServerName + ':' + inttostr(FServerPort) + AResource +
|
|
|
|
EncodeResourceParams(AResourceParams) + EncodeQueryStringParams
|
|
|
|
(FQueryStringParams);
|
|
|
|
|
|
|
|
if FNextRequestIsAsynch then
|
|
|
|
begin
|
|
|
|
Result := nil;
|
|
|
|
StartAsynchRequest(httpGET, url);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Result := SendHTTPCommand(httpGET, FAccept, FContentType, url, nil);
|
|
|
|
ClearAllParams;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.doPOST(AResource: string; AResourceParams: array of string)
|
|
|
|
: IRESTResponse;
|
|
|
|
var
|
|
|
|
s: string;
|
|
|
|
begin
|
|
|
|
try
|
|
|
|
Result := SendHTTPCommand(httpPOST, FAccept, FContentType,
|
|
|
|
'http://' + FServerName + ':' + inttostr(FServerPort) + AResource +
|
|
|
|
EncodeResourceParams(AResourceParams) + EncodeQueryStringParams
|
|
|
|
(FQueryStringParams), FBodyParams);
|
|
|
|
except
|
|
|
|
on E: EIdHTTPProtocolException do
|
|
|
|
begin
|
|
|
|
s := E.Message;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
ClearAllParams;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.doPOST(AResource: string; AResourceParams: array of string;
|
2013-11-05 14:57:50 +01:00
|
|
|
AJSONValue: TJSONValue; AOwnsJSONBody: Boolean): IRESTResponse;
|
2013-11-09 14:22:11 +01:00
|
|
|
begin
|
2013-11-11 12:23:49 +01:00
|
|
|
if not Assigned(AJSONValue) then
|
|
|
|
raise Exception.Create('AJSONValue is nil');
|
2013-11-09 14:22:11 +01:00
|
|
|
try
|
|
|
|
Result := doPOST(AResource, AResourceParams, AJSONValue.ToString);
|
|
|
|
finally
|
|
|
|
if AOwnsJSONBody then
|
|
|
|
FreeAndNil(AJSONValue);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-03-24 17:37:08 +01:00
|
|
|
function TRESTClient.doPATCH(AResource: string; AResourceParams: array of string; ABodyString: String): IRESTResponse;
|
|
|
|
var
|
|
|
|
url: string;
|
|
|
|
begin
|
|
|
|
url := 'http://' + FServerName + ':' + inttostr(FServerPort) + AResource +
|
|
|
|
EncodeResourceParams(AResourceParams) + EncodeQueryStringParams
|
|
|
|
(FQueryStringParams);
|
|
|
|
|
|
|
|
if FNextRequestIsAsynch then
|
|
|
|
begin
|
|
|
|
Result := nil;
|
|
|
|
StartAsynchRequest(httpPOST, url, ABodyString);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Result := SendHTTPCommandWithBody(httpPATCH, FAccept, FContentType, url,
|
|
|
|
ABodyString);
|
|
|
|
ClearAllParams;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.doPATCH(AResource: string; AResourceParams: array of string; AJSONValue: TJSONValue;
|
|
|
|
AOwnsJSONBody: Boolean): IRESTResponse;
|
|
|
|
begin
|
|
|
|
if not Assigned(AJSONValue) then
|
|
|
|
raise Exception.Create('AJSONValue is nil');
|
|
|
|
try
|
|
|
|
Result := doPATCH(AResource, AResourceParams, AJSONValue.ToString);
|
|
|
|
finally
|
|
|
|
if AOwnsJSONBody then
|
|
|
|
FreeAndNil(AJSONValue);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2013-11-09 14:22:11 +01:00
|
|
|
function TRESTClient.doPOST(AResource: string; AResourceParams: array of string;
|
|
|
|
ABodyString: String): IRESTResponse;
|
2013-10-30 00:48:23 +01:00
|
|
|
var
|
|
|
|
url: string;
|
|
|
|
begin
|
|
|
|
url := 'http://' + FServerName + ':' + inttostr(FServerPort) + AResource +
|
|
|
|
EncodeResourceParams(AResourceParams) + EncodeQueryStringParams
|
|
|
|
(FQueryStringParams);
|
|
|
|
|
|
|
|
if FNextRequestIsAsynch then
|
|
|
|
begin
|
|
|
|
Result := nil;
|
2013-11-09 14:22:11 +01:00
|
|
|
StartAsynchRequest(httpPOST, url, ABodyString);
|
2013-10-30 00:48:23 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
2013-11-09 14:22:11 +01:00
|
|
|
Result := SendHTTPCommandWithBody(httpPOST, FAccept, FContentType, url,
|
|
|
|
ABodyString);
|
|
|
|
ClearAllParams;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.doPUT(AResource: string; AResourceParams: array of string;
|
2013-11-11 12:23:49 +01:00
|
|
|
ABodyString: String): IRESTResponse;
|
2013-10-30 00:48:23 +01:00
|
|
|
var
|
|
|
|
url: string;
|
|
|
|
begin
|
|
|
|
url := 'http://' + FServerName + ':' + inttostr(FServerPort) + AResource +
|
|
|
|
EncodeResourceParams(AResourceParams) + EncodeQueryStringParams
|
|
|
|
(FQueryStringParams);
|
|
|
|
|
|
|
|
if FNextRequestIsAsynch then
|
|
|
|
begin
|
|
|
|
Result := nil;
|
2013-11-11 12:23:49 +01:00
|
|
|
StartAsynchRequest(httpPUT, url, ABodyString);
|
2013-10-30 00:48:23 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
2013-11-11 12:23:49 +01:00
|
|
|
Result := SendHTTPCommandWithBody(httpPUT, FAccept, FContentType, url,
|
|
|
|
ABodyString);
|
|
|
|
ClearAllParams;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.doPUT(AResource: string; AResourceParams: array of string;
|
|
|
|
AJSONValue: TJSONValue; AOwnsJSONBody: Boolean = true): IRESTResponse;
|
|
|
|
begin
|
|
|
|
if not Assigned(AJSONValue) then
|
|
|
|
raise Exception.Create('AJSONValue is nil');
|
|
|
|
|
|
|
|
try
|
|
|
|
Result := doPUT(AResource, AResourceParams, AJSONValue.ToString);
|
|
|
|
finally
|
|
|
|
if AOwnsJSONBody then
|
|
|
|
FreeAndNil(AJSONValue);
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.doPUT(AResource: string; AResourceParams: array of string)
|
|
|
|
: IRESTResponse;
|
|
|
|
begin
|
|
|
|
Result := SendHTTPCommand(httpPUT, FAccept, FContentType,
|
|
|
|
'http://' + FServerName + ':' + inttostr(FServerPort) + AResource +
|
|
|
|
EncodeResourceParams(AResourceParams) + EncodeQueryStringParams
|
|
|
|
(FQueryStringParams), FBodyParams);
|
|
|
|
ClearAllParams;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.EncodeResourceParams(AResourceParams
|
|
|
|
: array of string): string;
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
for i := low(AResourceParams) to high(AResourceParams) do
|
|
|
|
Result := Result + '/' + TIdURI.ParamsEncode(AResourceParams[i]);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.GetBodyParams: TStringlist;
|
|
|
|
begin
|
|
|
|
if not Assigned(FBodyParams) then
|
|
|
|
FBodyParams := TStringlist.Create;
|
|
|
|
Result := FBodyParams;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.GetConnectionTimeout: Integer;
|
|
|
|
begin
|
|
|
|
Result := FHTTP.ConnectTimeout;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.GetMultipartFormData: TIdMultiPartFormDataStream;
|
|
|
|
begin
|
|
|
|
if not Assigned(FMultiPartFormData) then
|
|
|
|
FMultiPartFormData := TIdMultiPartFormDataStream.Create;
|
|
|
|
Result := FMultiPartFormData;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.GetQueryStringParams: TStringlist;
|
|
|
|
begin
|
|
|
|
if not Assigned(FQueryStringParams) then
|
|
|
|
FQueryStringParams := TStringlist.Create;
|
|
|
|
Result := FQueryStringParams;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.GetRawBody: TStringStream;
|
|
|
|
begin
|
|
|
|
if not Assigned(FRawBody) then
|
|
|
|
FRawBody := TStringStream.Create('');
|
|
|
|
Result := FRawBody;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.GetSessionID: string;
|
|
|
|
begin
|
|
|
|
Result := Self.FLastSessionID;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.GetReadTimeout: Integer;
|
|
|
|
begin
|
|
|
|
Result := FHTTP.ReadTimeout;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.EncodeQueryStringParams(const AQueryStringParams: TStrings;
|
2013-11-05 14:57:50 +01:00
|
|
|
IncludeQuestionMark: Boolean = true): string;
|
2013-10-30 00:48:23 +01:00
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
if not Assigned(AQueryStringParams) or (AQueryStringParams.Count = 0) then
|
|
|
|
Exit;
|
|
|
|
if IncludeQuestionMark then
|
|
|
|
Result := '?';
|
|
|
|
|
|
|
|
for i := 0 to AQueryStringParams.Count - 1 do
|
|
|
|
begin
|
|
|
|
if i > 0 then
|
|
|
|
Result := Result + '&';
|
|
|
|
Result := Result + AQueryStringParams.Names[i] + '=' +
|
|
|
|
TIdURI.ParamsEncode(AQueryStringParams.ValueFromIndex[i]);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TRESTClient.HandleCookies;
|
|
|
|
var
|
2013-11-05 14:57:50 +01:00
|
|
|
s: string;
|
2013-10-30 00:48:23 +01:00
|
|
|
arr: TArray<string>;
|
|
|
|
begin
|
|
|
|
// Log('Received cookies', FHTTP.Response.RawHeaders.Text);
|
|
|
|
for s in FHTTP.Response.RawHeaders do
|
|
|
|
begin
|
|
|
|
if s.StartsWith('Set-Cookie', true) then
|
|
|
|
begin
|
|
|
|
arr := s.Split([':'], 2);
|
|
|
|
if arr[1].trim.StartsWith('dtsessionid') then
|
|
|
|
begin
|
|
|
|
arr := arr[1].Split(['='], 2);
|
|
|
|
FLastSessionID := TIdURI.URLDecode(arr[1].Split([';'])[0]);
|
|
|
|
end;
|
|
|
|
Break;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TRESTClient.HandleRequestCookies;
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
if Assigned(FHTTP.CookieManager) then
|
|
|
|
FHTTP.CookieManager.CookieCollection.Clear;
|
|
|
|
if not FLastSessionID.trim.IsEmpty then
|
|
|
|
FHTTP.Request.CustomHeaders.AddValue('Cookie',
|
|
|
|
'dtsessionid=' + FLastSessionID);
|
|
|
|
|
|
|
|
for i := 0 to FRequestHeaders.Count - 1 do
|
|
|
|
begin
|
|
|
|
FHTTP.Request.CustomHeaders.AddValue(FRequestHeaders.Names[i],
|
|
|
|
FRequestHeaders.ValueFromIndex[i]);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.HttpCommandToString(const AHttpCommand
|
|
|
|
: THttpCommand): string;
|
|
|
|
begin
|
|
|
|
case AHttpCommand of
|
|
|
|
httpGET:
|
|
|
|
Result := 'GET';
|
|
|
|
httpPOST:
|
|
|
|
Result := 'POST';
|
|
|
|
httpPUT:
|
|
|
|
Result := 'PUT';
|
|
|
|
httpDELETE:
|
|
|
|
Result := 'DELETE';
|
2013-11-05 14:57:50 +01:00
|
|
|
else
|
|
|
|
raise Exception.Create('Unknown HttpCommand in TRSF.HttpCommandToString');
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.ResetSession: TRESTClient;
|
|
|
|
begin
|
|
|
|
SessionID := '';
|
|
|
|
if Assigned(FHTTP.CookieManager) then
|
|
|
|
FHTTP.CookieManager.CookieCollection.Clear;
|
|
|
|
FHTTP.Request.RawHeaders.Clear;
|
|
|
|
Result := Self;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTClient.SendHTTPCommand(const ACommand: THttpCommand;
|
2013-11-05 14:57:50 +01:00
|
|
|
const AAccept, AContentType, AUrl: string; ABodyParams: TStrings)
|
2013-10-30 00:48:23 +01:00
|
|
|
: IRESTResponse;
|
|
|
|
begin
|
|
|
|
Result := TRESTResponse.Create;
|
|
|
|
FHTTP.Request.RawHeaders.Clear;
|
|
|
|
FHTTP.Request.CustomHeaders.Clear;
|
|
|
|
FHTTP.Request.Accept := AAccept;
|
|
|
|
FHTTP.Request.ContentType := AContentType;
|
|
|
|
HandleRequestCookies;
|
|
|
|
try
|
|
|
|
case ACommand of
|
|
|
|
httpGET:
|
|
|
|
begin
|
|
|
|
Result.Body.Position := 0;
|
|
|
|
FHTTP.Get(AUrl, Result.Body);
|
|
|
|
end;
|
|
|
|
|
|
|
|
httpPOST:
|
|
|
|
begin
|
|
|
|
if GetMultipartFormData.Size = 0 then
|
|
|
|
begin
|
|
|
|
Result.Body.Position := 0;
|
|
|
|
GetRawBody; // creates the body
|
|
|
|
FHTTP.Post(AUrl, FRawBody, Result.Body);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
FHTTP.Post(AUrl, GetMultipartFormData, Result.Body);
|
|
|
|
GetMultipartFormData.Clear;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
httpPUT:
|
|
|
|
begin
|
2013-11-09 14:22:11 +01:00
|
|
|
if GetMultipartFormData.Size <> 0
|
|
|
|
then { TODO -oDaniele -cGeneral : Rework please!!! }
|
2013-10-30 00:48:23 +01:00
|
|
|
raise Exception.Create('Only POST can Send Files');
|
|
|
|
Result.Body.Position := 0;
|
|
|
|
if Assigned(ABodyParams) and (ABodyParams.Count > 0) then
|
|
|
|
begin
|
|
|
|
GetRawBody.Size := 0;
|
|
|
|
GetRawBody.WriteString(EncodeQueryStringParams(ABodyParams, False));
|
|
|
|
end;
|
|
|
|
FHTTP.Put(AUrl, FRawBody, Result.Body);
|
|
|
|
end;
|
|
|
|
|
|
|
|
httpDELETE:
|
|
|
|
begin
|
|
|
|
Result.Body.Position := 0;
|
|
|
|
FHTTP.Delete(AUrl);
|
|
|
|
GetRawBody.Size := 0;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
on E: EIdHTTPProtocolException do
|
|
|
|
begin
|
|
|
|
Result.Body.WriteString(E.ErrorMessage);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
HandleCookies;
|
|
|
|
Result.SetResponseCode(FHTTP.Response.ResponseCode);
|
|
|
|
Result.SetResponseText(FHTTP.Response.ResponseText);
|
|
|
|
Result.SetHeaders(FHTTP.Response.RawHeaders);
|
|
|
|
end;
|
|
|
|
|
2013-11-09 14:22:11 +01:00
|
|
|
function TRESTClient.SendHTTPCommandWithBody(const ACommand: THttpCommand;
|
|
|
|
const AAccept, AContentType, AUrl: string; ABodyString: String): IRESTResponse;
|
2013-10-30 00:48:23 +01:00
|
|
|
begin
|
|
|
|
Result := TRESTResponse.Create;
|
|
|
|
FHTTP.Request.RawHeaders.Clear;
|
|
|
|
FHTTP.Request.CustomHeaders.Clear;
|
|
|
|
FHTTP.Request.Accept := AAccept;
|
|
|
|
FHTTP.Request.ContentType := AContentType;
|
|
|
|
HandleRequestCookies;
|
|
|
|
try
|
|
|
|
case ACommand of
|
|
|
|
httpGET:
|
|
|
|
begin
|
|
|
|
FHTTP.Get(AUrl, Result.Body);
|
|
|
|
end;
|
|
|
|
|
|
|
|
httpPOST:
|
|
|
|
begin
|
|
|
|
if GetMultipartFormData.Size <> 0 then
|
|
|
|
raise Exception.Create('This method cannot send files');
|
|
|
|
|
|
|
|
RawBody.Position := 0;
|
2013-11-09 14:22:11 +01:00
|
|
|
FRawBody.Size := 0;
|
|
|
|
FRawBody.WriteString(UTF8Encode(ABodyString));
|
2013-10-30 00:48:23 +01:00
|
|
|
FHTTP.Post(AUrl, FRawBody, Result.Body);
|
|
|
|
end;
|
|
|
|
|
2014-03-24 17:37:08 +01:00
|
|
|
httpPATCH:
|
|
|
|
begin
|
|
|
|
raise Exception.Create
|
|
|
|
('Sorry, PATCH is not supported by the RESTClient because is not supportd by the TidHTTP');
|
|
|
|
end;
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
httpPUT:
|
|
|
|
begin
|
|
|
|
RawBody.Position := 0;
|
2013-11-09 14:22:11 +01:00
|
|
|
FRawBody.Size := 0;
|
|
|
|
FRawBody.WriteString(UTF8Encode(ABodyString));
|
2013-10-30 00:48:23 +01:00
|
|
|
FHTTP.Put(AUrl, FRawBody, Result.Body);
|
|
|
|
end;
|
|
|
|
|
|
|
|
httpDELETE:
|
|
|
|
begin
|
|
|
|
FHTTP.Delete(AUrl);
|
|
|
|
RawBody.Size := 0;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
on E: EIdHTTPProtocolException do
|
|
|
|
begin
|
|
|
|
Result.Body.WriteString(E.ErrorMessage);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
HandleCookies;
|
|
|
|
Result.SetResponseCode(FHTTP.Response.ResponseCode);
|
|
|
|
Result.SetResponseText(FHTTP.Response.ResponseText);
|
|
|
|
Result.SetHeaders(FHTTP.Response.RawHeaders);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TRESTClient.SetAccept(const Value: string);
|
|
|
|
begin
|
|
|
|
FAccept := Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TRESTClient.SetBodyParams(const Value: TStringlist);
|
|
|
|
begin
|
|
|
|
FBodyParams := Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TRESTClient.SetConnectionTimeout(const Value: Integer);
|
|
|
|
begin
|
|
|
|
FHTTP.ConnectTimeout := Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TRESTClient.SetContentType(const Value: string);
|
|
|
|
begin
|
|
|
|
FContentType := Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TRESTClient.SetQueryStringParams(const Value: TStringlist);
|
|
|
|
begin
|
|
|
|
FQueryStringParams := Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TRESTClient.SetSessionID(const Value: string);
|
|
|
|
begin
|
|
|
|
FLastSessionID := Value;
|
|
|
|
if Assigned(FHTTP.CookieManager) then
|
|
|
|
FHTTP.CookieManager.CookieCollection.Clear;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TRESTClient.SetReadTimeout(const Value: Integer);
|
|
|
|
begin
|
|
|
|
FHTTP.ReadTimeout := Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TRESTClient.SetRequestHeaders(const Value: TStringlist);
|
|
|
|
begin
|
|
|
|
FRequestHeaders.Assign(Value);
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TRESTResponse }
|
|
|
|
|
|
|
|
function TRESTResponse.Body: TStringStream;
|
|
|
|
begin
|
|
|
|
Result := FBody;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTResponse.BodyAsJsonObject: TJSONObject;
|
2013-11-09 14:22:11 +01:00
|
|
|
begin
|
|
|
|
Result := BodyAsJsonValue as TJSONObject;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTResponse.BodyAsJsonValue: TJSONValue;
|
2013-10-30 00:48:23 +01:00
|
|
|
begin
|
|
|
|
try
|
2013-11-09 14:22:11 +01:00
|
|
|
if not Assigned(FBodyAsJSONValue) then
|
2013-10-30 00:48:23 +01:00
|
|
|
begin
|
|
|
|
if BodyAsString = '' then
|
2013-11-09 14:22:11 +01:00
|
|
|
FBodyAsJSONValue := nil
|
2013-10-30 00:48:23 +01:00
|
|
|
else
|
|
|
|
begin
|
|
|
|
try
|
2013-11-09 14:22:11 +01:00
|
|
|
FBodyAsJSONValue := TJSONObject.ParseJSONValue(BodyAsString);
|
|
|
|
// if Assigned(V) then
|
|
|
|
// FBodyAsJSONObject := V as TJSONObject;
|
2013-10-30 00:48:23 +01:00
|
|
|
except
|
2013-11-09 14:22:11 +01:00
|
|
|
FBodyAsJSONValue := nil;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
2013-11-09 14:22:11 +01:00
|
|
|
Result := FBodyAsJSONValue;
|
2013-10-30 00:48:23 +01:00
|
|
|
except
|
|
|
|
on E: Exception do
|
|
|
|
begin
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTResponse.BodyAsString: string;
|
|
|
|
var
|
|
|
|
ss: TStringStream;
|
|
|
|
begin
|
2013-11-05 14:57:50 +01:00
|
|
|
if FContentEncoding.IsEmpty then
|
|
|
|
FContentEncoding := 'UTF-8';
|
|
|
|
ss := TStringStream.Create('', TEncoding.GetEncoding(FContentEncoding));
|
2013-10-30 00:48:23 +01:00
|
|
|
try
|
|
|
|
FBody.Position := 0;
|
|
|
|
FBody.SaveToStream(ss);
|
|
|
|
Result := ss.DataString;
|
|
|
|
finally
|
|
|
|
ss.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TRESTResponse.Create;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
FHeaders := TStringlist.Create;
|
|
|
|
FBody := TStringStream.Create('', TEncoding.UTF8);
|
2013-11-09 14:22:11 +01:00
|
|
|
FBodyAsJSONValue := nil;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TRESTResponse.Destroy;
|
|
|
|
begin
|
2013-11-09 14:22:11 +01:00
|
|
|
FreeAndNil(FBodyAsJSONValue);
|
2013-10-30 00:48:23 +01:00
|
|
|
FHeaders.Free;
|
|
|
|
FBody.Free;
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2013-11-05 14:57:50 +01:00
|
|
|
function TRESTResponse.GetContentEncoding: String;
|
|
|
|
begin
|
|
|
|
Result := FContentEncoding;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTResponse.GetContentType: String;
|
|
|
|
begin
|
|
|
|
Result := FContentType;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTResponse.GetHeader(const Value: String): String;
|
|
|
|
var
|
|
|
|
s: String;
|
|
|
|
begin
|
|
|
|
if Assigned(FHeaders) and (FHeaders.Count > 0) then
|
|
|
|
begin
|
|
|
|
for s in FHeaders do
|
|
|
|
begin
|
|
|
|
if s.StartsWith(Value + ':', true) then
|
|
|
|
begin
|
|
|
|
Exit(s);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-03-31 11:25:16 +02:00
|
|
|
function TRESTResponse.GetHeaderValue(const Name: string): string;
|
|
|
|
var
|
|
|
|
s: string;
|
|
|
|
arr: TArray<string>;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
for s in Self.Headers do
|
|
|
|
begin
|
|
|
|
arr := s.Split([':'], 2);
|
|
|
|
if SameText(arr[0].trim, name) then
|
|
|
|
begin
|
|
|
|
Result := arr[1].trim;
|
|
|
|
Break;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
function TRESTResponse.Headers: TStringlist;
|
|
|
|
begin
|
|
|
|
Result := FHeaders;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTResponse.ResponseCode: Word;
|
|
|
|
begin
|
|
|
|
Result := FResponseCode;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRESTResponse.ResponseText: string;
|
|
|
|
begin
|
|
|
|
Result := FResponseText;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TRESTResponse.SetHeaders(AHeaders: TStrings);
|
2013-11-05 14:57:50 +01:00
|
|
|
var
|
|
|
|
CT: TArray<string>;
|
|
|
|
C: String;
|
2013-10-30 00:48:23 +01:00
|
|
|
begin
|
|
|
|
FHeaders.Assign(AHeaders);
|
2013-11-05 14:57:50 +01:00
|
|
|
|
|
|
|
C := GetHeader('content-type');
|
|
|
|
|
|
|
|
CT := C.Split([':'])[1].Split([';']);
|
2013-11-09 14:22:11 +01:00
|
|
|
FContentType := trim(CT[0]);
|
2013-11-05 14:57:50 +01:00
|
|
|
FContentEncoding := 'UTF-8'; // default encoding
|
|
|
|
if Length(CT) > 1 then
|
|
|
|
begin
|
|
|
|
if CT[1].trim.StartsWith('charset', true) then
|
|
|
|
begin
|
|
|
|
FContentEncoding := CT[1].trim.Split(['='])[1].trim;
|
|
|
|
end;
|
|
|
|
end;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TRESTResponse.SetResponseCode(AResponseCode: Word);
|
|
|
|
begin
|
|
|
|
FResponseCode := AResponseCode;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TRESTResponse.SetResponseText(AResponseText: string);
|
|
|
|
begin
|
|
|
|
FResponseText := AResponseText;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|