How to use an API with Postman?
Today, all web services are accessible via APIs. These are data providers that can be used with Postman. Postman is the most essential tool in the world of web development. Postman is a reference tool that you can use for PHP, Node.js, Ruby on Rails and Python development. All you have to do is interact with a APIand that's it. How to use an API with Postman? Find out here.
Table of contents
HTTP requests
Postman is a tool for making HTTP requests using a graphical interface. This makes the exercise much easier. All you have to do is enter the URL and the HTTP method. There are several HTTP methods that differ according to certain factors. These are the operation to be performed and the API used for it.
The most commonly used methods include:
- GET
- POST
- PUT
- PATCH
- DELETE
GET requests are those made by a browser when a URL is inserted in the navigation bar. Their purpose is to search for data or a page. POST requests send the information contained in the body of the request. It is sent to the server.
PUT requests are used to overwrite a resource with new data. They allow the data to be updated. PATCH requests are useful for updating a resource while modifying only the element sent in the body of the request.
Finally, the DELETE request is used to delete a resource. The uses of these requests vary depending on the server and the way it has been developed. These are very common methods for REST APIs.
HTTP requests sometimes have queryams. For those who know, these are data sent to the server by the URL, generally in GET requests. It's easy to define and modify these query params directly in the Postman interface. Using the tool, you can quickly modify query params or assign them to variables.
It is possible for HTTP requests to contain request headers. As with query params, Postman makes it easy to modify headers via its graphical interface.
The interest of certain requests lies in the fact that they send data to the server thanks to the request body. These are POST, PUT and PATCH. The data in the body that needs to be sent to the server via Postman can have different formats. One of the most commonly used formats is JSON. For SOAP APIs, there is XML.
With Postman, you can copy and paste a body into a text box. You can also choose the format you need. For a POST, PUT or PATCH request, the tool lets you edit the request body.
Some HTTP calls require caller authentication. You can set the necessary authorisations for your calls. To do this, access Postman's Authorization tab. On the tool, you will find various forms of authentication such as the simple API key, Basic Auth or OAuth.
The collections
As you develop, your API grows. In addition, the URLs to be tested by Postman increase. To help you organise and store requests, the tool provides you with collections. You can save and classify a series of HTTP requests.
How the calls are ordered in your collection is completely up to you. Postman also gives you the option of automatically executing all the calls in a collection in a certain order. It is therefore very important to define the order.
Using variables
During your development, you will have calls that depend on variables from other requests. For example, you may need to retrieve an id or token from a first call in order to verify the second.
To avoid copying and pasting every time you want to make a call, you can use Postman variables. These are used to store data received from a request so that it can be reused in future calls.
In addition, Postman variables are arranged in scopes. There are several of these, the main ones being:
- Global variable
- Environment variable
- collection variable
- Local variable
- given.
Variable scopes can be used in different collections. They can also be used to create environments with specific variables.
To define a variable, Postman provides the pm object, which contains the SET and GET functions. Depending on the scope of the variable to be defined, various formulas can be used.
Writing automated tests
When developing an API, you mustn't neglect unit and functional tests. They ensure that the API works properly. Postman also provides you with a test function. These are used to mock any call to an external API.
For the development of your backend, the tests should cover a certain logic. For example, if you use the Twitter API, you won't actually have to make calls to this service every time you run integration tests. You'll need to mock these calls.
You can have a suite of tests in Postman that will be able to call on all external services.
Building an API in Postman
As well as being able to manipulate and test an API, Postman also lets you build one. To do this, you need to use its API Builder. The API tab available on the tool is the central point of your API. It contains tests, collections and much more.
This tab allows you to centralise all the resources relating to your API, i.e. the documentation. Once the schema for each resource has been defined, Postman is able to generate collections automatically. Postman also offers a Mock Server so that you can test an API consumer.