VIDEO – QuickTest Pro – How to Test a REST Service

by Joe Colantonio on February 23, 2011

Post image for VIDEO – QuickTest Pro – How to Test a REST Service

In this video I show a quick example on how to use QuickTest Pro 11 and WinHttp to test a REST service. The QTP web-addin is not needed for this example.

Below is the code used for this example:

Set myRestBody = XMLUtil.CreateXMLFromFile("C:\body.xml") 
restRequest = myRestBody.ToString
url ="http://localhost:24240/HPFlights_REST/FlightOrders/"
contentType ="text/XML"
'1) Create reference to the WinHTTP request com object
 Set oWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1") 
 '2) Open a HTTP connection to a HTTP resource 
 oWinHttp.Open "POST", url, False
 '3) Add a HTTP request headers for endpoint and conent type
 oWinHttp.setRequestHeader "Content-Type", contentType 	oWinHttp.setRequestHeader "SOAPAction", url
'4) Send a HTTP request to the HTTP server with the header and body info
oWinHttp.Send restRequest
'5) Get response
 response = oWinHttp.ResponseText
'6) Close object reference 
Set oWinHttp = Nothing
'7) print the response
print response





{ 14 comments… read them below or add one }

shaan May 10, 2011 at 10:55 pm

hi joe,
i have a xml, public key and a private key. i would like to know if i can digitially sign this xml and then send it to a webservice using qtp. not the service test.
thank you

Reply

Joe Colantonio May 12, 2011 at 6:52 pm

shaan » Hi Shaan – I’m not sure how to do this. Have you taken a look at the AddAttachment or AddHeader Methods of the WebService object? Good Luck ~Joe

Reply

Sarma June 13, 2011 at 1:07 pm

Hi Joe
thanks for you code. I have to authenticate before
’4) Send a HTTP request to the HTTP server with the header and body info
oWinHttp.Send restRequest
How do i do that?

Thanks
Sarma

Reply

Joe Colantonio June 14, 2011 at 11:58 am

Sarma » Hi Sarma – you might want to try using the SetCredentials method. The SetCredentials method sets credentials to be used with an HTTP server, whether it is a proxy server or an originating server.

Const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0

WinHttpReq.SetCredentials “yourusername”, ourHTTPREQUEST_SETCREDENTIALS_FOR_SERVER

For more info check out: http://msdn.microsoft.com/en-us/library/aa384058(v=vs.85).aspx

Cheers,
Joe

Reply

bhuvan October 11, 2011 at 11:03 am

How to POST the REST webservice request which is available in xml format. Please do the needful on this.

Reply

Susu February 16, 2012 at 2:25 am

Hi Joe

if “contentType = application/x-www-form-urlencoded”, the request format should be Post Form, and how to send a request?

Susu

Reply

Suman May 21, 2012 at 1:07 pm

Hi Joe,

Very thank you for you Video on how to test Rest Service. I did what you suggested. I am getting Response in Json format, But I need it in XML format. Is I am doing any mistakes? Please let me know your comments?
For your information, I am getting response in XML format when i send the request through SOUP 3.6.1 service.

Reply

Joe Colantonio June 2, 2012 at 12:02 am

Suman » Hi – Did you set the contentType = “text/XML”?

Reply

M Sivateja June 5, 2012 at 9:16 am

hi Joe,

I am trying to test rest service.In response i am gettng internal error.please advice

Reply

Joe Colantonio June 9, 2012 at 8:05 pm

M Sivateja » Hi what rest service are you trying to test? Also is there anything else in the error msg or is it just internal error?

Reply

mamta October 31, 2012 at 10:03 am

hi

while running the same example i am getting error at line number 5… Invalid character
Line (5): “contentType =”application/json””.

can u please help on this

Reply

Joe Colantonio November 8, 2012 at 9:36 pm

mamta » This example does not use json – what REST service are you using?

Reply

Balu March 26, 2013 at 7:41 am

Hi Joe,

How can I send Authorization Key and Secret with my REST service?

Thanks,
Balu

Reply

Jas April 18, 2013 at 10:16 am

Hi Joe,
I am trying to automate rest services API using QTP, which returns the response in JSON format. What are the changes to be done on the above sample script to perform a Search operation ( GET). I have modified the content type= “application/json”. Any help on this is highly appreciated.

Thanks in Advance,
Jas

Reply

Leave a Comment

Previous post:

Next post: