How do I use Curl to upload files?

Curl is a powerful command-line tool used for transferring data to or from a server. You can use Curl to upload a file to a server using HTTP POST or PUT methods.

Here's how you can use Curl to upload a file:

HTTP POST method

The -F option is used for HTTP POST. The file is sent as a binary stream in a part of a multipart/form-data POST request.

curl -X POST -F "file=@/path/to/local/file.txt" http://hostname/resource

In this command, @ is used to specify the file's path. If you want to manually set the file's MIME type, you can use type= like this:

curl -X POST -F "file=@/path/to/local/file.txt;type=text/plain" http://hostname/resource

HTTP PUT method

The -T option is used for HTTP PUT. This will upload the file as a whole.

curl -X PUT -T /path/to/local/file.txt http://hostname/resource

Notes

  • Replace /path/to/local/file.txt with the path of your local file.
  • Replace http://hostname/resource with the URL of the server where you want to upload the file.

Keep in mind that the server must be configured to accept POST or PUT requests, otherwise, you will get an error.

Remember, Curl is a powerful tool with many options and features, so be sure to check out the man page (man curl) or the Curl website for more information.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon