Board index » delphi » TIdHTTP to Post XML request and get XML response

TIdHTTP to Post XML request and get XML response


2007-12-01 12:35:07 AM
delphi220
Objective: Submit to a URL an XML file and get an XML file in response.
Current function:
HTTP:TIdHTTP;
var
XMLRequest, XMLResponse : TStringStream;
HTTP.Post('https://apidemoeu.webex.com/WBXService/XMLService',XMLRequest,XMLResponse);
The response is saying the Document root element is missing.
Any suggestions or ideas?
Regards,
Rich
 
 

Re:TIdHTTP to Post XML request and get XML response

Rich Rohde writes:
Quote
var
XMLRequest, XMLResponse : TStringStream;



HTTP.Post('https://apidemoeu.webex.com/WBXService/XMLService',XMLRequest,XMLResponse);
Try this instead:
uses .., MSXML2_TLB, ComObj;
procedure TForm1.GetXML;
var
oXMLHTTP :IXMLHTTPRequest;
begin
oXMLHTTP := CreateOleObject('MSXML2.XMLHTTP') as IXMLHTTPRequest;
try
oXMLHTTP.open('POST',
https://apidemoeu.webex.com/WBXService/XMLService, False, '', '');
oXMLHTTP.send(EmptyParam);
finally
XMLDocument.LoadXML(Trim(oXMLHTTP.ResponseText));
oXMLHTTP := nil;
end;
end;
 

Re:TIdHTTP to Post XML request and get XML response

Actually, I made slight mistake try this instead. You can see how
things work better. This is tested...
uses ..., MSXML2_TLB, COMObj;
var
oXMLHTTP: IXMLHTTPRequest;
oXMLDoc: IXMLDOMDocument2;
begin
oXMLHTTP := CreateOleObject('MSXML2.ServerXMLHTTP') as
IXMLHTTPRequest;
try
oXMLDoc := CreateOleObject('MSXML2.DOMDocument.3.0') as
IXMLDOMDocument2;
try
oXMLHTTP.open('POST',
'https://apidemoeu.webex.com/WBXService/XMLService', False, '', '');
oXMLHTTP.send(EmptyParam);
finally
oXMLDoc.LoadXML(Trim(oXMLHTTP.ResponseText));
oXMLHTTP := nil;
end;
finally
ShowMessage(oXMLDoc.XML);
oXMLDoc := nil;
end;
end;
 

Re:TIdHTTP to Post XML request and get XML response

"Rich Rohde" <XXXX@XXXXX.COM>writes
Quote
The response is saying the Document root element is missing.
Is that what the actual server response data says, or is that what your
processing of the data says when you try to load it afterwards? What are
you actually doing with XMLResponse after Post() exits? Did you remember to
set the Position property back to 0 first?
Gambit
 

Re:TIdHTTP to Post XML request and get XML response

Eddie,
To clarify, I am using Delphi 2006. Where can I get MSXML2_TLB?
Rich
"Eddie Shipman" <XXXX@XXXXX.COM>writes
Quote
Actually, I made slight mistake try this instead. You can see how
things work better. This is tested...

uses ..., MSXML2_TLB, COMObj;

var
oXMLHTTP: IXMLHTTPRequest;
oXMLDoc: IXMLDOMDocument2;
begin
oXMLHTTP := CreateOleObject('MSXML2.ServerXMLHTTP') as
IXMLHTTPRequest;
try
oXMLDoc := CreateOleObject('MSXML2.DOMDocument.3.0') as
IXMLDOMDocument2;
try
oXMLHTTP.open('POST',
'https://apidemoeu.webex.com/WBXService/XMLService', False, '', '');
oXMLHTTP.send(EmptyParam);
finally
oXMLDoc.LoadXML(Trim(oXMLHTTP.ResponseText));
oXMLHTTP := nil;
end;
finally
ShowMessage(oXMLDoc.XML);
oXMLDoc := nil;
end;
end;

 

Re:TIdHTTP to Post XML request and get XML response

The Actual response is:
<?xml version="1.0" encoding="UTF-8"?>
<serv:message xmlns:serv="www.webex.com/schemas/2002/06/service"
xmlns:com="www.webex.com/schemas/2002/06/common">
<serv:header>
<serv:response>
<serv:result>FAILURE</serv:result>
<serv:reason>Document root element is missing.</serv:reason>
<serv:gsbStatus>PRIMARY</serv:gsbStatus>
<serv:exceptionID>999999</serv:exceptionID>
</serv:response>
</serv:header>
<serv:body/>
</serv:message>
The request is:
<?xml version="1.0" encoding="UTF-8"?>
<serv:message xmlns:xsi="www.w3.org/2001/XMLSchema-instance">
<header>
<securityContext>
<webExID>**********</webExID>
<password>********</password>
<siteID>********</siteID>
<partnerID>********</partnerID>
</securityContext>
</header>
<body>
<bodyContent
xsi:type="java:com.webex.service.binding.ep.GetAPIVersion"/>
</body>
</serv:message>
"Remy Lebeau (TeamB)" <XXXX@XXXXX.COM>writes
Quote

"Rich Rohde" <XXXX@XXXXX.COM>writes
news:XXXX@XXXXX.COM...

>The response is saying the Document root element is missing.

Is that what the actual server response data says, or is that what your
processing of the data says when you try to load it afterwards? What are
you actually doing with XMLResponse after Post() exits? Did you remember
to set the Position property back to 0 first?


Gambit


 

Re:TIdHTTP to Post XML request and get XML response

Additional information:
I'm using NativeXML to create the XMLRequest.
Rich
"Remy Lebeau (TeamB)" <XXXX@XXXXX.COM>writes
Quote

"Rich Rohde" <XXXX@XXXXX.COM>writes
news:XXXX@XXXXX.COM...

>The response is saying the Document root element is missing.

Is that what the actual server response data says, or is that what your
processing of the data says when you try to load it afterwards? What are
you actually doing with XMLResponse after Post() exits? Did you remember
to set the Position property back to 0 first?


Gambit


 

Re:TIdHTTP to Post XML request and get XML response

"Rich Rohde" <XXXX@XXXXX.COM>writes
Quote
The Actual response is:
There is clearly a root element in that data.
You did not answer my question about how you are using XMLResponse after
Post() exits. So I am guessing that you are passing it as-is to a function
that takes a TStream as input, and did not set the Position property to 0
beforehand. Am I right?
Gambit
 

Re:TIdHTTP to Post XML request and get XML response

Quote
You did not answer my question about how you are using XMLResponse after
Post() exits. So I am guessing that you are passing it as-is to a
function that takes a TStream as input, and did not set the Position
property to 0 beforehand. Am I right?

I'm not sure I really understand your question. Before the post the
XMLResponse is created:
XMLResponse := TStringStream.Create('');
After the post, I am using NativeXML to format the XML to a TString and
showing the information in a TMemo control.
Rich
 

Re:TIdHTTP to Post XML request and get XML response

Rich Rohde writes:
Quote
Eddie,

To clarify, I am using Delphi 2006. Where can I get MSXML2_TLB?

Rich
"Eddie Shipman" <XXXX@XXXXX.COM>wrote in
message news:XXXX@XXXXX.COM...
>Actually, I made slight mistake try this instead. You can see how
>things work better. This is tested...
>
>uses ..., MSXML2_TLB, COMObj;
>
>var
>oXMLHTTP: IXMLHTTPRequest;
>oXMLDoc: IXMLDOMDocument2;
>begin
>oXMLHTTP := CreateOleObject('MSXML2.ServerXMLHTTP') as
>IXMLHTTPRequest;
>try
>oXMLDoc := CreateOleObject('MSXML2.DOMDocument.3.0') as
>IXMLDOMDocument2;
>try
>oXMLHTTP.open('POST',
>'https://apidemoeu.webex.com/WBXService/XMLService', False, '', '');
>oXMLHTTP.send(EmptyParam);
>finally
>oXMLDoc.LoadXML(Trim(oXMLHTTP.ResponseText));
>oXMLHTTP := nil;
>end;
>finally
>ShowMessage(oXMLDoc.XML);
>oXMLDoc := nil;
>end;
>end;
>
Import the type library.
or possibly:
#import "msxml3.dll"
using namespace MSXML2;
 

Re:TIdHTTP to Post XML request and get XML response

"Rich Rohde" <XXXX@XXXXX.COM>writes
Quote
I'm not sure I really understand your question.
What exactly are you doing with the XMLResponse stream after Post() finishes
its work? Please show that actual code. I don't know how to be any clearer
than that.
Quote
After the post, I am using NativeXML to format the XML to
a TString and showing the information in a TMemo control.
But you did not show any of the code you are using to do all of that. You
haven't shown anything you are doing after calling Post(). I have never
looked at NativeXML, so I do not know what kind of input it takes. Does it
accept a TStream object or a String value, for instance? It makes a
difference.
Gambit
 

Re:TIdHTTP to Post XML request and get XML response

Got it! Just need a little push.
Okay, generated the code as shown and I am getting nothing with some errors.
Here's the code I used:
procedure TWAMF.GetXML;
var
XMLHTTP : IXMLHTTPRequest;
XMLDoc : IXMLDomDocument2;
const
URL = 'https://apidemoeu.webex.com/WBXService/XMLService';
begin
XMLHTTP := CreateOleObject('MSXML2.ServerXMLHTTP') as IXMLHTTPRequest;
try
XMLDoc := CreateOleObject('MSXML2.DOMDocument.3.0') as
IXMLDOMDocument2;
try
XMLHTTP.open('POST', URL, False, '', '');
XMLHTTP.Op
finally
XMLDoc.LoadXML(Trim(XMLHTTP.ResponseText));
XMLHTTP := nil;
end;
finally
ShowMessage(XMLDoc.xml);
XMLDoc := nil;
end;
end;
 

Re:TIdHTTP to Post XML request and get XML response

Here's the code that usess the TIdHTTP:
function TWAMF.GetXMLstr: boolean;
var
strXML : string;
i : integer;
sl : TStrings;
XMLRqst : string;
XMLResponse : TStringStream;
XMLRequest : TStringStream;
const
URL = 'https://apidemoeu.webex.com/WBXService/XMLService';
begin
sl := TStringList.Create;
XMLRequest := TStringStream.Create('');
XMLResponse := TStringStream.Create('');
GetVersion(XMLRequest);{ This routine uses NativeXML to create XML
Request }
FormatXML(sl,XMLRequest);
memXML.Lines.Text := sl.Text;
memXML.Lines.Add('<<<<<<<< Response Follows>>>>>>>>>>');
SSLIO.SSLOptions.Method := sslvSSLv3;
HTTP.ReadTimeout := 10000;{ IdTimeoutInfinite; }
HTTP.ConnectTimeout := 10000;
HTTP.IOHandler := SSLIO;
sl.Text := XMLRqst;
HTTP.Request.ContentType := 'text/xml';
HTTP.HTTPOptions := [];
XMLResponse.Position := 0;
XMLRequest.Position := 0;
HTTP.Post(URL,XMLRequest, XMLResponse);
HTTP.Disconnect;
memXML.Lines.Text := memXML.Lines.Text + XMLResponse.DataString;
sl.Free;
XMLRequest.Free;
XMLResponse.Free;
end;
Here's the data in the TMemo field:
<?xml version="1.0" encoding="UTF-8"?>
<serv:message xmlns:xsi="www.w3.org/2001/XMLSchema-instance">
<header>
<securityContext>
<webExID>***</webExID>
<password>***</password>
<siteID>***</siteID>
<partnerID>***</partnerID>
</securityContext>
</header>
<body>
<bodyContent
xsi:type="java:com.webex.service.binding.ep.GetAPIVersion"/>
</body>
</serv:message>
<<<<<<<< Response follows>>>>>>>>>>
<?xml version="1.0" encoding="UTF-8" ?>
<serv:message xmlns:serv="www.webex.com/schemas/2002/06/service"
xmlns:com="www.webex.com/schemas/2002/06/common">
<serv:header>
<serv:response>
<serv:result>FAILURE</serv:result>
<serv:reason>Document root element is missing.</serv:reason>
<serv:gsbStatus>PRIMARY</serv:gsbStatus>
<serv:exceptionID>999999</serv:exceptionID></serv:response>
</serv:header>
<serv:body>
</serv:body>
</serv:message>
If I comment out the GetVersion function, the following shows in the Memo:
<<<<<<<<<< Response Follows>>>>>>>>>>
<?xml version="1.0" encoding="UTF-8" ?><message
xmlns="www.webex.com/schemas/2002/06/service"><header><response><result>SUCCESS</result><reason>:
WebEx XML API V4.0.9, WebEx XML API V4.1.0, WebEx XML API V3.8.2, WebEx
XML API V4.0.1, WebEx XML API V4.0.5, WebEx XML API V4.2.0, WebEx XML API
V4.0.10, WebEx XML API V3.9.1, WebEx XML API
V4.0.7</reason></response></header><body><bodyContent/></body></message>
 

Re:TIdHTTP to Post XML request and get XML response

"Rich Rohde" <XXXX@XXXXX.COM>writes
Quote
Here's the code that usess the TIdHTTP:
Since the error is coming from the server's valid response, that means you
are sending invalid XML in your request.
Quote
Here's the data in the TMemo field:
Look very carefully at your request data. When you are using GetVersion(),
the "serv" namespace is not being defined anywhere in the request XML, like
it is in the response XML, so your document element is not valid.
Gambit
 

Re:TIdHTTP to Post XML request and get XML response

Rich Rohde writes:
Quote
Here's the code that usess the TIdHTTP:

function TWAMF.GetXMLstr: boolean;
var
strXML : string;
i : integer;
sl : TStrings;
XMLRqst : string;
XMLResponse : TStringStream;
XMLRequest : TStringStream;
const
URL = 'https://apidemoeu.webex.com/WBXService/XMLService';
begin
sl := TStringList.Create;
XMLRequest := TStringStream.Create('');
XMLResponse := TStringStream.Create('');

GetVersion(XMLRequest);{ This routine uses NativeXML to create XML
Request } FormatXML(sl,XMLRequest);
memXML.Lines.Text := sl.Text;
memXML.Lines.Add('<<<<<<<< Response Follows>>>>>>>>>>');
SSLIO.SSLOptions.Method := sslvSSLv3;
HTTP.ReadTimeout := 10000;{ IdTimeoutInfinite; }
HTTP.ConnectTimeout := 10000;
HTTP.IOHandler := SSLIO;
sl.Text := XMLRqst;
HTTP.Request.ContentType := 'text/xml';
HTTP.HTTPOptions := [];
XMLResponse.Position := 0;
XMLRequest.Position := 0;
HTTP.Post(URL,XMLRequest, XMLResponse);
HTTP.Disconnect;
memXML.Lines.Text := memXML.Lines.Text + XMLResponse.DataString;
sl.Free;
XMLRequest.Free;
XMLResponse.Free;
end;

Here's the data in the TMemo field:

<?xml version="1.0" encoding="UTF-8"?>
<serv:message xmlns:xsi="www.w3.org/2001/XMLSchema-instance">
<header>
<securityContext>
<webExID>***</webExID>
<password>***</password>
<siteID>***</siteID>
<partnerID>***</partnerID>
</securityContext>
</header>
<body>
<bodyContent
xsi:type="java:com.webex.service.binding.ep.GetAPIVersion"/></body>
</serv:message>
<<<<<<<< Response follows>>>>>>>>>>
<?xml version="1.0" encoding="UTF-8" ?>
<serv:message
xmlns:serv="www.webex.com/schemas/2002/06/service"
xmlns:com="www.webex.com/schemas/2002/06/common">
<serv:header><serv:response><serv:result>FAILURE</serv:result>
<serv:reason>Document root element is missing.</serv:reason>
<serv:gsbStatus>PRIMARY</serv:gsbStatus>
<serv:exceptionID>999999</serv:exceptionID></serv:response>
</serv:header>
<serv:body>
</serv:body>
</serv:message>
Indy <i>may</i>be encoding you XML in the request.
Is there a way to check, on your server end, the actual
request text? If it has a bunch of < and>in it,
then that is the problem.
What do the GetVersion and FormatXML function do?
Seems it is going to be easier and more reliable for you to use
XMLHTTPRequest.