Marcio Cunha

How to use curl to test APIs and HTTP headers effectively

Master the command-line tool for HTTP requests. Learn how to simulate verbs, send JSON payloads, and debug API responses with curl.

Marcio Cunha•2 min
Also available in:PortuguêsEspañol
Summary
  • The curl command is the industry standard for interacting with servers via HTTP directly from the terminal.
  • Detailed inspection of HTTP headers helps identify authentication failures and configuration issues in APIs.
  • Verbs like GET, POST, PUT, and DELETE are simulated via flags, enabling agile testing of data flows.
  • Sending JSON payloads via curl requires the correct Content-Type header to ensure proper server interpretation.
  • Testing requests manually prevents common errors that often cause CORS blocking in frontend applications.

Why use curl for API testing?

Curl is a powerful command-line tool used to transfer data between a client and a server. If you are building or integrating systems, it is your best ally. Unlike browsers, which hide a lot of information, curl shows you exactly what happens in the 'conversation' between your machine and the API. Mastering it means you don't have to rely solely on heavy GUI tools to understand why an integration failed.

Understanding HTTP request anatomy

Every HTTP request sent via curl consists of three main parts: the method (the verb), the headers (metadata), and the body (the payload). When you run curl -v https://api.example.com, the -v flag puts it in 'verbose' mode, detailing the entire handshake. This is essential for diagnosing network issues, similar to how we investigate latency with tcpdump, ensuring packets reach their destination.

Simulating verbs and sending JSON data

To send data, we change the default GET method to POST or PUT and tell the server the format is JSON. Curl makes this easy with simple flags. When sending JSON, it is vital to set the correct header so the backend processes the request as expected. See how to do it:

curl -X POST https://api.example.com/users 
-H 'Content-Type: application/json' 
-d '{"name": "Marcio", "role": "Engineer"}'

If you are setting up your backend to accept these requests, you may need to adjust permissions, as detailed in our guide on how to fix CORS errors.

Inspecting headers and response codes

Often, an API returns an error, but the browser hides the details. With curl, you can extract just the response headers using -I. This helps verify authentication tokens or cache keys without downloading the response body. Analyzing codes like 200 (OK), 401 (Unauthorized), or 500 (Internal Server Error) is the first step for any technical fix.

Conclusion

Mastering curl transforms your ability to debug systems. It is an agnostic tool that runs on almost any Linux or macOS server. By getting used to testing your API routes directly from the terminal, you gain speed and a much clearer view of your code's behavior.

Remember that curl is just the first layer of testing. Whenever possible, automate these tests to ensure future changes do not break existing functionality, maintaining the stability of your architecture.