Friday, 16 July 2021

How to test a Rest API without any tools like postman

How to test a Rest API without Postman

Most of the time, we have the need to test the functionality of an API without a third-party tool, as installing an API testing software is not always allowed in an organization.

As we all know, web applications that interact with APIs generally use technologies like javascript, jquery, Ajax to do all the CRUD operations(Create, Read, Update, Delete) on data within the website.

Since web browsers are able to execute the javascript, no other software is required to be installed on the machine in order to query an API.

Let's get started with a small piece of code that can get the API data at the click of a button.

To demonstrate this, I am using the below public rest API:

http://jsonplaceholder.typicode.com/users/1

you can simply browse the above url to see the JSON data which we are going to fetch using our script.

Implementation: Create an HTML file TestApi.html as shown below and run it with any web browser.

1. Get Request (Without Authentication): 

TestApi.html



<html>
	<head>
		<title>Test API with JavaScript</title>
	<script>
		function callAPI(){
		var url="http://jsonplaceholder.typicode.com/users/1";		
		var method="GET";
		var xhreq=new XMLHttpRequest();
		xhreq.onload=function(){
			document.getElementById("userdata").innerHTML = this.responseText;			
			}
		xhreq.open(method,url);
		xhreq.send();
		}

	</script>
	</head>
	<body>
		<br>
		<center>
		Click the button get json data<br><br>
		<button onclick="callAPI()" class="btn" style="background-color:green; color:white;"> Get Data </button>
		</center>
		<p id="userdata"></p>
	</body>
</html>

The output of the above code looks like this:

After clicking the Get Data Button, json data is loaded on the web page as shown below:

 The output json data we are expecting is displayed on the screen.That's it.

                                            Thank you!

                      

No comments:

Post a Comment

How to Integrate GraphQL API with Blue Prism

 GraphQL API - Blue Prism Integration This post discusses how to integrate GraphQL API with Blue Prism using a sample graphQL API and Utilit...