* Added XML beautification
This commit is contained in:
parent
6033ea1046
commit
f1cf1e16b6
|
@ -26,7 +26,9 @@ uses
|
||||||
SysUtils, Classes, strutils, IniFiles,
|
SysUtils, Classes, strutils, IniFiles,
|
||||||
{$ifdef use_synapse}httpsend, ssl_openssl,{$endif}
|
{$ifdef use_synapse}httpsend, ssl_openssl,{$endif}
|
||||||
{$ifdef use_fclweb}fphttpclient,{$endif}
|
{$ifdef use_fclweb}fphttpclient,{$endif}
|
||||||
JTemplate, fpjson, jsonparser;
|
JTemplate,
|
||||||
|
fpjson, jsonparser,
|
||||||
|
DOM, XMLRead, XMLWrite;
|
||||||
|
|
||||||
var
|
var
|
||||||
data: TextFile;
|
data: TextFile;
|
||||||
|
@ -42,6 +44,9 @@ var
|
||||||
sessionIni: TIniFile;
|
sessionIni: TIniFile;
|
||||||
beautify: Boolean;
|
beautify: Boolean;
|
||||||
|
|
||||||
|
type
|
||||||
|
TContentType = (ctOther, ctJSON, ctXML);
|
||||||
|
|
||||||
procedure CmdAskUser(AName: String);
|
procedure CmdAskUser(AName: String);
|
||||||
var
|
var
|
||||||
value, default: String;
|
value, default: String;
|
||||||
|
@ -88,6 +93,18 @@ begin
|
||||||
{$endif}
|
{$endif}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function IdentifyContentType(AString: String; out ContentType: TContentType): Boolean;
|
||||||
|
begin
|
||||||
|
if Pos('application/json', AString) > 0 then
|
||||||
|
ContentType := ctJSON
|
||||||
|
else if Pos('application/xml', AString) > 0 then
|
||||||
|
ContentType := ctXML
|
||||||
|
else
|
||||||
|
ContentType := ctOther;
|
||||||
|
|
||||||
|
Result := (ContentType <> ctOther);
|
||||||
|
end;
|
||||||
|
|
||||||
procedure ProcessCall(AURL: String);
|
procedure ProcessCall(AURL: String);
|
||||||
var
|
var
|
||||||
s: String;
|
s: String;
|
||||||
|
@ -96,6 +113,8 @@ var
|
||||||
{$endif}
|
{$endif}
|
||||||
jsonParser: TJSONParser;
|
jsonParser: TJSONParser;
|
||||||
jsonData: TJSONData;
|
jsonData: TJSONData;
|
||||||
|
contentType: TContentType;
|
||||||
|
xmlDoc: TXMLDocument;
|
||||||
begin
|
begin
|
||||||
parser.Content := AURL;
|
parser.Content := AURL;
|
||||||
parser.Replace;
|
parser.Replace;
|
||||||
|
@ -171,13 +190,27 @@ begin
|
||||||
|
|
||||||
response.Position := 0;
|
response.Position := 0;
|
||||||
|
|
||||||
if beautify and (Pos('application/json', http.GetHeader(http.ResponseHeaders, 'Content-Type')) > 0) then
|
if beautify and IdentifyContentType(http.GetHeader(http.ResponseHeaders, 'Content-Type'), contentType) then
|
||||||
|
begin
|
||||||
|
case contentType of
|
||||||
|
ctJSON:
|
||||||
begin
|
begin
|
||||||
jsonParser := TJSONParser.Create(response);
|
jsonParser := TJSONParser.Create(response);
|
||||||
jsonData := jsonParser.Parse;
|
jsonData := jsonParser.Parse;
|
||||||
writeln(jsonData.FormatJSON);
|
writeln(jsonData.FormatJSON);
|
||||||
jsonData.Free;
|
jsonData.Free;
|
||||||
jsonParser.Free;
|
jsonParser.Free;
|
||||||
|
end;
|
||||||
|
ctXML:
|
||||||
|
begin
|
||||||
|
ReadXMLFile(xmlDoc, response);
|
||||||
|
response.Size := 0;
|
||||||
|
WriteXMLFile(xmlDoc, response);
|
||||||
|
response.Position := 0;
|
||||||
|
content.LoadFromStream(response);
|
||||||
|
writeln(content.Text);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
end else
|
end else
|
||||||
begin
|
begin
|
||||||
content.LoadFromStream(response);
|
content.LoadFromStream(response);
|
||||||
|
|
Loading…
Reference in New Issue