Linux command line tool for uploading files over HTTP as multipart/form-data?

1,332

Solution 1

Use curl:

curl -F "file=@localfile;filename=nameinpost" url.com

Solution 2

It is possible to do this with wget only. At least with version 1.13.4 and maybe others. The --post-file option allows you to specify a file to send, so long as the postdata file is constructed properly.

I have also tested this with binary files and it works as expected. You do NOT need to base64 encode the file, but you do need to ensure your file does not contain the boundary.

The minimum command required to make this work would be:

wget --header="Content-type: multipart/form-data boundary=FILEUPLOAD" --post-file postfile http://domain/uploadform

and the postdata file would need to contain something like:

--FILEUPLOAD
Content-Disposition: form-data; name="comment"

I love uploading files!

--FILEUPLOAD
Content-Disposition: form-data; name="uploadFile"; filename="myfile.bin"; 
Content-Type: application/octet-stream
Media Type: application/octet-stream

Give me some automated file upload action!

--FILEUPLOAD--

A number of details are important here:

  1. Lines in the post data file are terminated with \r\n. The only exception is data inside the file context.
  2. Every BOUNDARY attribute in the postdata must match the BOUNDARY value in the call to wget. (FILEUPLOAD in the example)
  3. All boundaries are prefixed with two hyphens "--" and terminated with \r\n
  4. The last boundary is suffixed with two extra hyphens "--" and terminated with \r\n
  5. Each piece of data, file content or parameter value, is surrounded by an empty line "\r\n"

I thought this might help someone since some controlled environments have wget but not curl.

Share:
1,332

Related videos on Youtube

user882196
Author by

user882196

Updated on September 17, 2022

Comments

  • user882196
    user882196 over 1 year

    I have a java project.How can i generate UML diagrams of that ptoject using AgroUML.

    I downloaded agroUML but the UI is very confusing. I thought might be it will be something like import package and than generate UML diagram for that package.

    Anybody has any idea about how to use AgroUML UI to generate UML diagrams if you already have source code.?

  • Chloe
    Chloe about 11 years
    I could not get wget to work with a binary file. I created the text portion of the post data file, saved, used cat pic.jpg >> postdata, loaded in Notepad++, and appended the last boundary + '--' + EOL. W3C Reference
  • tu-Reinstate Monica-dor duh
    tu-Reinstate Monica-dor duh almost 11 years
    @Chloe, I had to revisit this for another project, and this time binary files were needed and found it worked as expected. I constructed a simple php file upload page and then did it through a browser to first get the file size and then compared it to the result from using wget. I'd suggest you do the same since a stray newline can cause the whole process to fail.
  • FlappySocks
    FlappySocks over 6 years
    A semicolon is missing. It caused problems with python tornado --header="Content-type: multipart/form-data; boundary=FILEUPLOAD"
  • achimh
    achimh almost 4 years
    I could not make this work at all (missing semicolon or not). I finally resorted to a static build of curl at github and that did the job.