This Step by Step Tutorial Explains How To Use Postman For Testing Different API Formats like REST, SOAP and GraphQL with Examples:
In the last tutorial we learned how to install and setup the Postman Tool. This tutorial will focus on how to use the tool for executing and testing endpoints.
You will also get to know how to simply import and export requests to and from Postman and use them with the other common formats as well.
=> Take A Look At The Postman Beginners Guide Here.
Table of Contents:
How To Use Postman?
A new request can be directly created through the Postman console or imported through various other ways i.e. Through a curl request, Swagger or Open API spec, etc.
Here is a Video Tutorial:
Let’s look at each of these ways in detail!!
Importing & Exporting Requests Through Curl
CURL is a command-line utility to execute any endpoint request. It is less readable as the output is visible on the terminal only and lacks formatting. Postman provides an ability to import any CURL request as a Postman Request which can later be enhanced and makes it extremely readable.
#1) To illustrate CURL, we would be using a sample REST endpoint using a fake REST service (This provides a lot of different endpoints to run a test or use it as a fake server hosting the REST endpoints). We will be using a GET endpoint.
#2) The CURL request for the above endpoint will be
curl https://reqres.in/api/users?page=2
#3) Now let’s try importing this request into Postman. Open Postman Application -> Click Import -> Select “Paste Raw text” option in the Import window.
#4) Now paste the curl command/request in the Import window and click Import.
#5) If the Curl request syntax was correct, the Import should be successful and you should see the request turning into Postman request. You can try hitting Send on the imported request and match the output with that of the curl output.
#6) To export the request back to CURL, simply go to the “CODE” option in the Postman request window and select the type/language that you want to export (Postman provides a lot of different bindings/clients to which the requests can be exported to. For this example, we will use cURL to get the request exported as cURL request).
Importing Requests Through Open API Specification
Open API based Specification has gained popularity recently and has almost become the defacto standard for representing REST-based API documentation. It’s good in a way as it follows standards and can be interpreted by different tools, languages in the same way in which it makes it an abstract choice to represent any API
Open API Yaml or JSON to Postman collection feature is a great feature provided by Postman (It might not be available in the older versions though) and it allows you to directly import the entire API through the swagger specification.
The steps remain pretty much the same as we did with importing the CURL requests in the above section.
Let’s discuss those steps once again.
#1) Take Open API based specification for an API. For illustration purposes, we will use the open-source “pets api swagger” documentation or specification.
You can simply navigate to Petstore and download the swagger JSON (please note that Open API based specifications are available in both YAML as well as JSON formats and both are interconvertible. However, Postman supports both of them).
#2) Now, you can import the specification to Postman collection via 3 ways.
- Directly through the file present on the local system i.e. the downloaded JSON.
- By specifying the remote swagger link without even having to download the file locally.
- By simply copying the contents of the Open API Specification (in YAML/JSON format) and “Pasting as raw text” in the Postman Import window.
#3) Once the Open API Spec is imported into Postman, you would be able to see a new Postman collection that got created with all the requests that were present in the spec. Not only the collection but also associated collection variables, request body, etc also get imported and get created as valid Postman requests.
We will look into Postman collections in detail in our upcoming tutorials.
Rest Vs SOAP Endpoints
With Postman, you can test all kinds of API – be it REST API or SOAP-based web services. In our previous tutorial, we saw how we can create requests for REST API.
Now, let’s see how we can use Postman to validate SOAP-based web services.
Currently, not many developments happen with SOAP as REST is the most defacto standard that’s currently being used, however, there are a lot of legacy projects still using SOAP and a lot of people still use tools like SOAP UI to test sending requests and receiving responses from SOAP-based web services.
With a tool like Postman, it becomes quite easy if we could use it for SOAP-based services as well. Let’s see how we could do it. We will be using a sample deployed calculator Web Service WSDL to test it.
The service lists down basic calculator operations i.e. Add, Subtract, Multiply & Divide each having two inputs and returning an output based on the operation or request selected.
For Executing the request in Postman, follow the steps below:
#1) Create a new request in Postman. Select HTTP Method as POST and enter the web service endpoint as the URL.
#2) In the request body, select the encoding type as XML (text/XML) and enter the request body (as you would have done in other tools like SOAP UI).
#3) Fill in the request parameters. Let’s test the addition method from the web service and add 2 integer inputs.
#4) Hit Send to execute the request and validate the web service result. You could validate the Response body, Header, Status code, etc. in the same way as you would do for REST-based web services.
GraphQL Endpoints
With the latest version of the Postman application (>7.3.2), there is now support to test GraphQL based endpoints as well. GraphQL is one of the relatively new paradigms for API development, where a client tells what all fields it expects in the response body, rather than an API request resource itself constraining the response format.
To know more about GraphQL refer here.
GraphQL is an upcoming query language/protocol, similar to a REST endpoint, with a major difference being no fixed response/request body i.e. both the Request as well as Response will depend on the requesting client. Let’s try to understand how we can go about testing GraphQL based API using the POSTMAN tool.
We will use a sample deployed GraphQL endpoint for testing out on the Postman tool. The endpoint that we are going to use is https://countries.trevorblades.com. It is an exposed GraphQL endpoint that returns a list of Continents, Countries, Country details, etc. as per the given query.
The best part about GraphQL endpoint is that it’s always associated with documentation that is generally written using the GraphQL schema language.
To learn more details about the GraphQL documentation, visit the above link and navigate to the documentation section on the right side of the browser window and you can get all the details about query fields available, all models and request types supported.
We will test with a simple request that would query all the continents list against the given endpoint.
Refer the below steps, to use Postman to query GraphQL:
#1) In the URL field, add the GraphQL endpoint and select the HTTP method as POST.
#2) Now in the body tab for Postman request, select GraphQL as Request type, and enter GraphQL Request body as shown below.
{ continents { code, name } }
#3) Now let’s try hitting the send button, to see the response from the GraphQL server.
You can see the below response.
"data": { "continents": [ { "code": "AF", "name": "Africa" }, { "code": "AN", "name": "Antarctica" }, { "code": "AS", "name": "Asia" }, { "code": "EU", "name": "Europe" }, { "code": "NA", "name": "North America" }, { "code": "OC", "name": "Oceania" }, { "code": "SA", "name": "South America" } ] } }
Given below is the image of how the response looks in the Postman window.
Note: Please note that GraphQL support in Postman is available only for version above 7.2
Conclusion
In this tutorial, we touched upon executing and testing endpoints of various types like REST, SOAP, GRAPHQL through the Postman application. The ability to hit different web service types through Postman makes it a defacto tool for performing different types of validation and ubiquitous choice.
Suggested read =>> Publishing Pact Contract with Postman
We also discussed how we can import requests of different types directly as Postman requests and build tests further from there.
Request imports and exports are essentially a very useful feature as it does support a lot of standard conversion means which have a great acceptance. Example: CURL is a standard command-line utility to play with any kind of requests through the command line Open API Spec which is becoming the standard way of expressing documentation, etc.
In our upcoming tutorials, we will discuss the advanced concepts of Postman like Collections, Variables, Assertions, etc.
=> Read Through The Easy Postman Training Series.