cURL: How to send POST requests and upload files

Updated: February 1, 2024 By: Guest Contributor Post a comment

Introduction

For many developers and system administrators, cURL is an invaluable tool for testing HTTP requests, including POST requests, and for uploading files to a server. cURL, which stands for ‘Client URL’, is a command-line tool and library that can send requests to and fetch data from servers, using a number of different protocols such as HTTP, HTTPS, FTP, and more.

Understanding cURL POST Requests

POST requests are used when you want to send data to a server to create/update a resource. They are one of the most common HTTP requests used in APIs, web forms, and more.

To send a POST request with cURL, you’ll use the -X POST option to specify the request type and the -d or --data option to include the data you want to send. Here’s a basic example of how to send a POST request:

curl -X POST -d 'param1=value1&param2=value2' https://example.com/resource

If you have a substantial amount of data to send or if you prefer to use a file for your data, you can utilize the -d option along with the @filename syntax in the following way:

curl -X POST -d @/path/to/data/file https://example.com/resource

This allows you to send data from the specified file to the specified resource using the curl command.

Content-Type Header

When you send POST requests, it’s important to set the Content-Type header using the -H option when necessary. For example, if you’re sending JSON data, you’ll want to let the server know that the data should be treated as JSON:

curl -X POST -H 'Content-Type: application/json' -d '{"param1":"value1", "param2":"value2"}' https://example.com/resource

Uploading Files with cURL

To upload files, cURL offers the -F option, which allows you to transmit files in the form of ‘multipart/form-data’, which is the same format used when you submit files through a web form.

curl -F 'file=@/path/to/your/file.txt' https://example.com/upload
curl -F 'file=@/path/to/your/file.txt' -F 'key1=value1' -F 'key2=value2' https://example.com/upload

Advanced File Upload

If you want to provide a different file name than the actual one, or need to specify the file’s Content-Type, you can do so within the -F argument:

curl -F 'file=@/path/to/your/file.txt;filename=myfile.txt;type=text/plain' https://example.com/upload

Using HTTPS and Authentication

cURL can also handle HTTPS connections and authentication. To use cURL with HTTPS, no extra option is needed; cURL will use HTTPS by default if the URL is specified as such. However, if the server you’re sending your request to uses SSL certificates that aren’t recognized by cURL, you may need to use the --insecure flag to ignore SSL warnings, although this is not recommended for production environments due to security reasons.

If your POST request requires authentication, use the -u option:

curl -u username:password -X POST -d 'data' https://example.com/resource

Conclusion

Now that you’ve learned to send POST requests and upload files with cURL, you have a formidable tool in your development toolkit. Whether you’re working with APIs or handling file uploads and downloads, understanding cURL commands will streamline your workflow and make you more effective at interacting with servers.

Remember to exercise caution when using cURL, particularly when dealing with sensitive data and passwords. Avoid using the –insecure flag when working with confidential information, and consider using token-based authentication instead of passing credentials directly whenever possible.