Curl

Curl

Introduction

Using curl to test a web API is a common practice among developers, as it provides a quick and flexible way to send HTTP requests from the command line. curl is a command-line tool available on most Unix-based systems (including Linux and macOS) and Windows, used to transfer data to or from a server. It supports various protocols, including HTTP, HTTPS, FTP, and more.

Here’s a guide on how to use curl for testing a web API:

1. Basic GET Request

To send a GET request to an API endpoint, you would use curl followed by the URL. For example:

curl https://api.example.com/data

This command retrieves data from the specified URL.

2. Including Headers

Often, you need to include headers in your request. For instance, to include an Authorization header for a token or to specify the content type, use the -H option:

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

3. POST Request with Data

To send data to the server, such as creating a new record via a POST request, use the -d option to include the data payload:

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

This sends a JSON payload to the server.

4. PUT and DELETE Requests

Similarly, for PUT (update) and DELETE requests, specify the method with -X:

# PUT request 
curl -X PUT -H "Content-Type: application/json" -d '{"name":"John",  "age":31}'
https://api.example.com/users/1 

# DELETE request 
curl -X DELETE https://api.example.com/users/1

5. Handling Cookies

If you need to send or receive cookies, use the -b option to send cookies and -c to store the cookies returned from the server:

# Send cookies 
curl -b "name=value" https://api.example.com/data 

# Store cookies 
curl -c cookies.txt https://api.example.com/data

6. Verbose and Debugging

For debugging, the -v (verbose) option is useful as it displays the request and response headers:

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

7. Saving Response to a File

To save the response to a file, use the -o option:

curl -o output.txt https://api.example.com/data

8. Using Secure Connections

For secure connections (HTTPS), curl automatically uses SSL. You can specify SSL options with --ssl, though in most cases, curl handles SSL without extra input.

9. Timeouts

For setting timeouts, use the --connect-timeout option to specify the maximum time allowed for the connection to the server:

curl --connect-timeout 10 https://api.example.com/data

10. Following Redirects

If the API might redirect, use -L to tell curl to follow redirects:

curl -L https://api.example.com/data

Conclusion

curl is a powerful tool for testing web APIs. It allows you to quickly send various types of requests, inspect responses, and interact with APIs in a straightforward manner.

As a command-line tool, it’s also convenient for automating tests and integrating into scripts.

You can download curl for just about every operating system from here: – https://curl.se/download.html

Stephen

Hi, my name is Stephen Finchett. I have been a software engineer for over 30 years and worked on complex, business critical, multi-user systems for all of my career. For the last 15 years, I have been concentrating on web based solutions using the Microsoft Stack including ASP.Net, C#, TypeScript, SQL Server and running everything at scale within Kubernetes.

Finchett.com
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.