Getty Images

Tip

How to interact with network APIs using cURL, Postman tools

Network engineers can use cURL and Postman tools to work with network APIs. Use cases include getting interface information and manipulating device configuration data.

APIs are simplifying the way network engineers work in the industry. They govern the high-tech world, from smartphones and smartwatches to the International Space Station and data centers across the globe.

Engineers who have worked with APIs before likely won't return to the traditional days of a command-line interface (CLI) and endless syntaxes, which often produce errors that result in network outages and revenue loss for businesses. APIs are the new CLIs.

In my previous article about network API use cases, I overviewed what APIs are, their use cases and why network engineers should adopt them in a world where agility is key to delivering top-notch services. This article explores how network engineers can use tools like cURL and Postman to interact with network APIs.

What are cURL and Postman?

Most network engineers have heard about cURL and Postman in various networking and developer communities. Postman is a GUI-based API testing platform with more than 20 million users around the world. It helps developers design, build, test and improve their APIs with more iterations. Using the tool simplifies API workflows in testing and production environments.

The command-line tool cURL is used to send data to and from the server. The tool is widely used because of its rich features, such as multiprotocol support -- especially HTTP and HTTPS.

To use Postman and cURL, network engineers have to determine which HTTP verb to use in order to interact with infrastructures. One way to do so is to link HTTP verbs to create read, update and delete (CRUD) functions that databases use.

HTTP VERB CRUD ACTION

POST

CREATE

GET

READ

PUT

UPDATE

PATCH

UPDATE

DELETE

DELETE

Transfer data, build APIs and automate network tasks

In networking, Secure Shell (SSH) is a widely used protocol for secure remote access to network devices. But with the increasing need for automation and the use of APIs for network devices, network engineers need an interface to interact with the device APIs in a programmable way.

HTTP or HTTPS protocols can interact with network device APIs using CLI and GUI tools, such as cURL and Postman, respectively. These interactions can get data or manipulate data. In the networking field, data is mostly device configurations. These tools help engineers integrate network devices with other network management systems as well improve network efficiency and automate networking processes.

Let's jump in and interact with a device API via HTTPS using both cURL and Postman. Note: You must understand device API documentation clearly through the CRUD operations common for all APIs.

The examples below show different scenarios in which network engineers can use cURL and the Postman GUI in a sandbox or production environment.

The tasks include the following:

  • Log in to a router.
  • Get interface information in JSON.
  • Log in to a sandbox via HTTP.
  • Manipulate an interface to change an IP address.
  • Shut down an interface.
  • Get a Python code snippet from Postman.

Interact using cURL command-line utility

1. Log in to a router

Network engineers always need to log in to network devices, such as switches and routers. To do so, engineers can access a remote network using a sandbox CLI and log in with an IP address. In the example below, we log in to a router with an IP address of 172.31.0.1 via HTTPS using the following command:

curl -k https://172.31.0.1/restconf/ -u "cisco:cisco"
Screenshot showing cURL command to log in to router
Log in to router.

Here "-k" ignores Secure Sockets Layer certification, and "-u" is included to mention the username with password.

Using cURL is an effective way to access network devices remotely, as it supports multiple protocols.

2. Get information on the interface

The next step is to collect interface information, such as the primary address, description and forwarding information. This information is helpful for network engineers as they analyze and troubleshoot networks.

To inspect an interface on a Cisco device, engineers typically use the following Cisco IOS command:

'show interface <interface name>'

Engineers can use cURL to do the same task. One option is to get the interface information in JSON. Currently, the JSON data format is used as a standard to exchange data, and many programming languages and tools support it. JSON is both human-readable and machine-parsable. So, engineers might need interface information in JSON format to integrate it with an API or use in network automation.

Engineers can collect the interface information in JSON by adding a JSON accept header, as seen below with the following command:

curl -H "Accept: application/yang-data+json" -k 
https://172.31.0.1/restconf/data/cisco-IOS-XE-
native:native/interface/gigabitethernet=1 -u "cisco":"cisco"
Screenshot showing interface information in JSON
Get interface information in JSON.

3. Change the IP address of an interface

Network engineers encounter changing needs in their daily and monthly tasks. In some cases, they might have to edit router configurations or change an interface IP address.

This example shows how engineers can use a cURL command to change an IP address via the CLI. Most of the time, network engineers use SSH to access devices remotely. But new networking standards and tools provide alternatives, such as the cURL command below:

$ curl -X PATCH -H "Content-Type: application/yang-data+json" -H 
"Accept: application/yang/data+json" -d '{Cisco-IOS-XE-native: 
GigabitEthernet": [{"name": "1", "shutdown": true}]}' -k 
https://172.31.0.1/restconf/data/Cisco-IOS-XE-
native:native/interface/GigabitEthernet=1 -u "cisco":"cisco"
Screenshot showing cURL command to change interface IP address
Use cURL command to change interface IP address.

Interact using Postman GUI

1. Log in to Cisco DevNet sandbox via HTTP

Network engineers can use the Postman GUI to interface with one of the Cisco sandbox routers. The tool is often easy to use compared to cURL.

Screenshot showing Cisco DevNet sandbox
Log in to Cisco DevNet sandbox.

2. Get information on the interface

Engineers might need to check the status of an interface to troubleshoot or verify the configuration on a router. In this case, they can use the Postman GUI via the HTTPS protocol.

Here, we use the GET verb to get information or to perform a READ action, using the following command in the Postman request bar:

GET - https://172.31.0.1/restconf/data/Cisco-IOS-XE-native:native/interface

Navigate the Authorization and Headers tabs in Postman to fill out parameters and ensure accurate information.

Screenshot showing interface information in Cisco DevNet sandbox
Get interface information in Cisco DevNet sandbox.

3. Manipulate the interface to change the IP address

Network engineers can use the POST method to manipulate the interface.

POST - https://172.31.0.1/restconf/data/Cisco-IOS-XE-native:native/interface

In the example below, I demonstrate the configuration editing capability via HTTPS, changing the IP address of the interface using the Postman GUI.

Screenshot showing how to manipulate an interface
Manipulate an interface to change IP address.

4. Shut down an interface

Network pros can use the PATCH method to make a change or shut down an interface via the HTTPS protocol and Postman. The Body tab in Postman must contain the configuration needed to shut down the interface in JSON format.

Network engineers can deactivate an interface for a router or switch if, for example, the team doesn't need to use it for a period of time. Shutting down an interface is like locking a door for more security.

In the example below, I show how it's possible to operate tasks in Postman, which engineers commonly perform with CLI. All engineers need is the credentials to access the sandbox via HTTP, as shown in the Postman request bar.

Screenshot showing how to shut down the interface
Shut down the interface.

A unique Postman feature is the ability to get a snippet of Python code to perform functions from the Postman tool. Because Python is such a common coding language -- thanks to its ease of use, flexibility and rich set of libraries and frameworks -- engineers often need to get the code to manage and operate network infrastructure.

Engineers can get the Python code from Postman to decrease manual effort and increase agility when using it according to project requirements.

Screenshot showing how to use Postman to get a Python code snippet
Get a Python code snippet in Postman.

Benefits and challenges of cURL and Postman

No tool is perfect. Both cURL and Postman have their benefits and challenges when using network APIs. For example, cURL is good for beginners, but it doesn't allow users to save requests. Meanwhile, Postman supports any programming language, but it uses a lot of RAM.

Below is a breakdown of the pros and cons for cURL and Postman.

Comparison showing the pros and cons of cURL and Postman
Compare the pros and cons of cURL and Postman.

Multiple tools are available in the industry that help network engineers and developers interact with network APIs. While the tools aren't perfect, engineers can choose one that suits their needs and helps them get their jobs done quickly.

Next Steps

Use cURL and Wget to download network files from CLI

 How to use the LFTP client to download files

Dig Deeper on Network management and monitoring