How to Append Data to a URL Using cURL

This article explains how to append data to a URL using the curl command-line tool. You will learn how to pass data as query parameters for GET requests, how to format data automatically using built-in curl flags, and how to properly handle URL encoding for special characters.

1. Manually Appending Data to the URL (GET Requests)

The most straightforward way to append data to a URL is to manually construct a query string and attach it to the end of the URL using a question mark (?) followed by key-value pairs separated by ampersands (&).

When using this method in your terminal, always wrap the URL in quotes to prevent the shell from interpreting the ampersand (&) as a background process command.

curl "https://api.example.com/search?query=laptop&limit=10"

2. Automatically Appending Data with --get and --data

Instead of manually constructing long URL strings, you can use curl to append the data for you. By combining the --get (or -G) option with the --data (or -d) option, curl will take the specified data key-value pairs and automatically append them to the end of the URL with the correct ? and & separators.

curl -G --data "query=laptop" --data "limit=10" https://api.example.com/search

This command sends a GET request to https://api.example.com/search?query=laptop&limit=10.

3. Handling Special Characters with --data-urlencode

If your data contains spaces, symbols, or special characters, sending them raw in a URL can cause errors. You can use the --data-urlencode option to force curl to format and encode the data properly before appending it to the URL.

curl -G --data-urlencode "user=John Doe" --data-urlencode "status=active&ready" https://api.example.com/users

In this example, the space in “John Doe” is converted to %20 (or +), and the & in the status is safely encoded so it is not mistaken for a parameter separator.

4. Appending Data in POST Requests

If you need to send data to a URL using a POST request instead of a GET request, do not use the -G flag. By default, using the -d or --data flag will send the data inside the request body rather than appending it to the URL.

curl -d "username=john" -d "password=secret" https://api.example.com/login