Curl Pretty

5 min read Oct 12, 2024
Curl Pretty

Making Your Command Line Output Beautiful: Mastering the Art of curl with --pretty

Ever found yourself staring at a wall of text when using curl to fetch data from an API? It can be overwhelming, especially when you're dealing with complex JSON responses. Fear not! The --pretty option in curl is your secret weapon for making your output beautiful and easily readable.

What is curl --pretty?

curl --pretty is a powerful tool for formatting the output of curl commands, especially when dealing with JSON responses. It allows you to transform raw JSON data into a more human-readable format, making it much easier to analyze and understand.

Why Use curl --pretty?

  • Clarity: The --pretty option organizes your output in a structured, indented format, making it easy to understand the hierarchy and relationships within your JSON data.
  • Readability: The output becomes much more visually appealing, simplifying the task of finding specific information within a large response.
  • Efficiency: By presenting data in a clear and organized way, curl --pretty saves you time and effort when it comes to analyzing and interpreting responses.

How to Use curl --pretty

The basic syntax is straightforward:

curl --pretty-print 

Replace <URL> with the target API endpoint you want to fetch data from. This command tells curl to format the output using a standard, easy-to-read indentation.

Beyond the Basics: Advanced Formatting Options

curl --pretty offers a range of customization options to tailor the output to your specific needs. Here are some examples:

Formatting with -F:

curl --pretty-format "%{url}: %{http_code} - %{time_total:.2f}s - %{size_download}" 

This example formats the output to display the URL, HTTP status code, total request time, and download size.

Formatting with JSON Path Expressions:

curl --pretty-format "[%{@.name}, %{@.age}]" 

This command uses JSON Path expressions to extract specific data elements from your JSON response. In this example, it retrieves the values of "name" and "age" from the JSON.

Using a Custom Format:

You can also define your own custom format using a template string:

curl --pretty-format "{\\\"url\\\": \\\"%{url}\\\", \\\"http_code\\\": \\\"%{http_code}\\\", \\\"size_download\\\": \\\"%{size_download}\\\"}"  

This example constructs a JSON object with the desired fields and their values.

Examples

Example 1: Fetching Data from a Weather API:

curl --pretty-print https://api.openweathermap.org/data/2.5/weather?q=London&appid=

This command retrieves weather data for London and formats the output in a clear, indented JSON format.

Example 2: Extracting Specific Information:

curl --pretty-format "[%{@.results[0].name}, %{@.results[0].address}]" https://maps.googleapis.com/maps/api/place/textsearch/output=json?query=Starbucks+New+York&key=

This command fetches location information for Starbucks in New York City and extracts the name and address of the first result using JSON Path expressions.

Conclusion

curl --pretty is a game-changer for interacting with APIs. It transforms raw, unreadable output into beautiful, easily understandable information. By leveraging --pretty's options, you can customize the output to meet your specific requirements, making your command line interactions with APIs a breeze.