Sling Academy
Home/DevOps/cURL: How to send POST requests and upload files

cURL: How to send POST requests and upload files

Last updated: February 01, 2024

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.

Next Article: cURL: How to Add Headers and Params When Making HTTP Requests

Previous Article: How to switch users in Ubuntu

Series: Linux Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide