NewQODEX QA Services for API teams.Learn more →
Automation Testing4 min read

Top 10 Curl Commands: A Guide for Developers

S
Content Team
Top 10 Curl Commands: A Guide for Developers

In today's interconnected world, API testing and HTTP requests have become essential skills for developers. One of the most powerful command-line tools for these tasks is curl. Whether you're debugging an API, downloading files, or testing your web application, curl offers versatility that few other tools can match.

This guide covers the top 10 most commonly used curl commands that every developer should know, with practical examples and explanations to help you master this essential tool.

1. Basic GET Request

The simplest and most common use of curl is making a GET request to fetch data from a URL:

curl https://example.com

This command retrieves the HTML content of the specified website. By default, curl outputs the response body to your terminal.

Key options to enhance basic GET requests:

  • -i or --include: Includes HTTP headers in the output

  • -s or --silent: Makes curl silent, hiding the progress meter and error messages

Example with headers:

curl -i https://api.github.com

2. Downloading Files

Curl excels at downloading files from the internet with its simple yet powerful syntax:

curl -O https://example.com/file.zip

The -O option saves the file with its original filename in your current directory. For more control over the download:

curl -o custom_name.zip https://example.com/file.zip

When dealing with interrupted downloads, curl can resume where it left off:

curl -C - -O https://example.com/large-file.iso

The -C - option tells curl to automatically figure out where to resume the download from.

3. POST Requests

Sending data to web servers is another common curl task. To make a POST request with form data:

curl -X POST -d "name=John&email=john@example.com" https://example.com/submit

For JSON data, which is common in modern APIs:

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"name":"John","email":"john@example.com"}' \
  https://api.example.com/users

JSON data can also be sent from a file, which is useful for complex payloads:

curl -X POST -H "Content-Type: application/json" -d @data.json https://api.example.com/users

4. Authentication

Many APIs require authentication. Curl supports various authentication methods, with basic auth being the simplest:

curl -u username:password https://api.example.com/secure

For bearer token authentication, commonly used in OAuth 2.0 flows:

curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" https://api.example.com/resources

This method is widely used when working with modern APIs from services like GitHub, Twitter, or your own secured endpoints.

5. Custom Headers

Setting HTTP headers is essential when working with APIs:

curl -H "User-Agent: MyApp/1.0" https://api.example.com

Multiple headers can be specified for more complex requests:

curl -H "Content-Type: application/json" \
     -H "Accept: application/json" \
     -H "Authorization: Bearer token123" \
     https://api.example.com/data

Headers help you control how your request is processed and what format responses should be in.

6. Follow Redirects

Many websites use redirects. By default, curl doesn't follow them, but you can enable this behavior:

curl -L https://shortened-url.com

The -L option makes curl follow redirects automatically. You can also limit the number of redirects:

curl -L --max-redirs 3 https://example.com

This prevents curl from following endless redirect loops, which can happen with misconfigured websites.

7. Verbose Output

When troubleshooting API calls or understanding how a website responds, verbose mode is invaluable:

curl -v https://api.example.com

For even more detailed information, especially about SSL/TLS handshakes:

curl --trace output.txt https://api.example.com

The verbose output shows:

  • Request headers sent by curl

  • Response headers received from the server

  • SSL/TLS negotiation details

  • Data transfers

This information is crucial when debugging connection issues or API problems.

8. Request Timeouts

When working with potentially slow servers, setting timeouts prevents your script from hanging indefinitely:

curl --connect-timeout 10 https://slow-server.com

You can also set a maximum time for the entire operation:

curl --max-time 30 https://slow-server.com/large-file

Setting appropriate timeouts is essential in automated scripts to handle network issues gracefully.

9. Using Proxy Servers

For testing or accessing region-restricted content, proxy support comes in handy:

curl -x http://proxy-server:8080 https://example.com

For SOCKS proxies, which are often used with SSH tunnels:

curl --socks5 localhost:1080 https://example.com

Proxies can help you test your applications from different geographic locations or bypass network restrictions.

10. Saving Response to a File

Instead of displaying the response in the terminal, you might want to save it to a file:

curl -o response.json https://api.example.com/data

For more complex scenarios, you can split headers and body into separate files:

curl -D headers.txt -o body.html https://example.com

This approach is useful when you need to analyze responses thoroughly or process them with other tools.

Conclusion

Mastering these ten curl commands will significantly enhance your development workflow. While curl offers hundreds of options, these core commands cover most day-to-day scenarios developers encounter.

For those looking to dive deeper, the official curl documentation is comprehensive and well-maintained. You might also consider exploring tools built on top of curl, such as Postman or Insomnia, which offer graphical interfaces for API testing while leveraging curl's powerful capabilities underneath.

What's your favorite curl command or trick? Share in the comments below!

Additional Resources