How to convert a Postman request to curl
If you're developing an API, you're probably already familiar with Postman. It is a great help during development when we want a quick and easy-to-use way to build and send API requests. But since this is a desktop GUI application, you cannot use it in all cases. For example, in a server environment or when running automated tests, you can't use Postman. In these cases, a cURL command is much more usable.
Fortunately, the developers of Postman have also thought about this problem. They implemented a Code module for the application. With this module, you can easily convert requests into different formats. But let's start presenting the concrete steps.
Converting Postman request to cURL
- Start Postman and open a request
- Just for sure, check if it really works
- Click on the small code icon in the top right corner of the application window.
- Postman converts your request and displays it in the window that opens.
The generated cURL command is:curl --location 'https://api.publicapis.org/entries?category=science'
- Copy and test the command
Other options
cURL parameterization:
If you want to change the basic parameters of the cURL command, you can do so by clicking on the small gear icon. As you change the settings, the generated command will change immediately.
Convert to other formats:
In addition to the cURL command, you can convert Postman requests to many other formats. For example, to PHP code using the Guzzle library, to a plain JavaScript fetch call, or even to a Python request.
The generated JS code:
var myHeaders = new Headers();
myHeaders.append("my-header-item", "header-value");
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
fetch("https://api.publicapis.org/entries?category=science", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));