Posting file to pastebin via Batch with CURL

I am attempting to upload log files to pastebin using CURL.exe on a Windows 10 machine in a batch (.bat) file.

The following command works:

curl -X POST -d "api_dev_key=MY_DEV_KEY" -d "api_paste_code=this is a the text to send to pastebin" -d "api_option=paste" "https://pastebin.com/api/api_post.php" -d "api_expire_date=10M"

However, it only allows uploading a single line, where as I want to upload an entire file.
I tried:

curl -X POST -d "api_dev_key=MY_DEV_KEY" -T "logfile.txt" -d "api_option=paste" "https://pastebin.com/api/api_post.php"

But curl responds to this with "Bad API request, invalid api_dev_key".

I've found numerous options such as Easy way to paste command line output to paste bin services?, POSTing a file's contents with CURL, and Bash Script to upload files and data to Pastebin.com that allow you to either upload a file's contents to pastebin or pipe a command to curl as a post on unix / bash, but none of these work in batch do to a difference in handling pipes.

What is the proper way to post a logfile to pastebin from within a batch file?


Solution 1:

You're trying to make a multipart POST request (just as the API requires), but the -T option actually tells curl to use a PUT request instead. PUT is used exclusively for uploading a single file and nothing else; it does not allow for multiple fields. (That's what POST is for.) It does not make sense to mix the two.

So because you're working with a form, the file's contents also have to be submitted as a form field. The field name is api_paste_code, and the value can be loaded from a file using curl's < or @ operators:

-d "api_paste_code=<logfile.txt"

In this case, you most likely should be using -F instead of -d everywhere, as the former generates a MIME multipart message that better supports large files (especially binary files), while the latter tries to put everything in a single urlencoded string.

-F "api_dev_key=MY_DEV_KEY" -F "api_paste_code=<logfile.txt" -F "api_option=paste"

(Generally the API service should accept both, although it's certainly possible that only -d will work.)