Forum Discussion
Currently I'm building up system to test our REST Api's just using requests library in python.
This is success and more flexible than SOAP UI (for me)
can add libraries like this
only down side is it's functional testing only
let me know if need more info
- Colin_McCrae9 years agoCommunity Hero
I just use an ServerXMLHTTP object. (https://msdn.microsoft.com/en-us/library/ms766431(v=vs.85).aspx)
This is just for sending to a RESTful API during tests. There are limits to what you can do and check with it. But if you just need to drive things via an API and interpret the responses, it does the job fine.
NisHera - I've been using the Requests library for Python in something outside of TestComplete and it is indeed a very nice HTTP handler. Makes things VERY simple to use. Only drawback I've found is that by making everything so simple, it sometimes goes a little too far. If a request fails, it seems to automatically try again - which I don't always want. And it can hit the usual problem in Python where a connection can get "stuck" open (although his happens a LOT more using urllib2). But I wrap the whole thing in an Eventlet to stop a connection getting stuck permanently. That, and the default logging level for the library is set way too high. But that's easy to turn down. (Just make sure you also set the same level for the urllib3 library - which it uses - as it's that that generates a lot of the unwanted "info" level log messages.)
- ykrrishna9 years agoOccasional Contributor
NisHeraColin_McCraeAlexKaraskevin_kapell
Thanks for the reply.
I do have imported the required libraries earlier but I am getting a timeout error.
Is it ok if I ask for the code samples for how you are handling your request and response from the URLs ? As I am also working on REST I hope that will be a great help
Thanks All
- Colin_McCrae9 years agoCommunity Hero
Mine is VBScript ...
' Set up the POST Set json_object = CreateObject("MSXML2.ServerXMLHTTP") json_object.open "POST", <URL-OF-ENDPOINT>, False json_object.setRequestHeader "Content-Type", "application/json" ' Send the JSON json_object.send <JSON-DATA> ' Grab the reply code from the server reply = json_object.status
I use it as send only. (Hence always using "POST") The response is a simple response code. Which I normally expect to be a 200. And of course, it triggers some actions from the service it hits which are reflected on the website I run the tests against.
With a "GET" request, you get a JSON/XML blob in the response which you can parse.
<URL-OF-ENDPOINT> is the API endpoint you want to hit.
<JSON-DATA> is a string I build in code. It's only relevant to the application I'm testing obviously.
Pretty straightforward. More reading here: https://msdn.microsoft.com/en-us/library/ms766431(v=vs.85).aspx
- NisHera9 years agoValued Contributor
def getResponse(self,urlResource): # urlResource is the url for rest service try: headers = {'cmkey':self.sessionToken} # this is to send security token in my rest app response = requests.get(urlResource, headers=headers) if response.status_code!=requests.codes.ok: raise RestError("Request to GET failed with code := "+str(response.status_code)) except RestError as e: Log.Warning("Responce Error ",e.msg) finally: if response.text == "" : responsetx = response.status_code else: responsetx = response.text return responsetx
this is simplified function I wrote in general library class.
If it helps you..............