Search code examples
curlpostasp.net-web-apigoogle-apicherrypy

I cannot upload my file via POST with cURL


I'm trying to upload firmware to a computer via API, which normally I do through the click of a button on our web server. This is the 'General' header:

Request URL:http://192.168.243.179:8080/firmware/linux/
Request Method:POST
Status Code:200 OK
Remote Address:192.168.243.179:8080
Referrer Policy:no-referrer-when-downgrade

On the firmware page, I would click a button and it uploads my firmware file to /firmware/linux/.

This is my response header when I submit the firmware file:

HTTP/1.1 200 OK
Date: Tue, 11 Apr 2017 23:22:43 GMT
Content-Length: 134
Content-Type: text/html;charset=utf-8
Server: CherryPy/3.2.2

Here is my request header:

POST /firmware/linux HTTP/1.1
Host: 192.168.243.179:8080
Connection: keep-alive
Content-Length: 63067756
Cache-Control: max-age=0
Origin: http://192.168.243.179:8080
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
Content-Type: multipart/form-data; boundary=----
WebKitFormBoundarynaUDhUWIArqOTvuC
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://192.168.243.179:8080/firmware/
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6

and my request payload:

------WebKitFormBoundarynaUDhUWIArqOTvuC
Content-Disposition: form-data; name="image"; filename="uImage-jjr-dvr-
atlas_v1.0.1-16-g5e31aec"
Content-Type: application/octet-stream

------WebKitFormBoundarynaUDhUWIArqOTvuC--

I am using the following cURL command to imitate these requests to POST my firmware file over to the computer:

curl -i -X POST -d @"C:\Users\name\Documents\firmware/firmwarefile" http://192.168.243.179:8080/firmware/linux/ -H "Content-Type: multipart/form-data"

The above yields the error ValueError: Invalid boundary in multipart form: ''

I then try it with a boundary as supplied in my request header:

curl -i -X POST -d @"C:\Users\name\Documents\firmware/firmwarefile" http://192.168.243.179:8080/firmware/linux/ -H 
"Content-Type: multipart/form-data; boundary=----WebKitFormBoundarynaUDhUWIArqOTvuC"

The above command yields no errors, but I then check if the firmware has been updated, and it hasn't.

Am I posting my file correctly? I don't understand why the firmware file is not being pushed through.

Thanks for the help in advance.


Solution

  • I have gotten help from the official forums for cURL and I have received an e-mail in which has solved my problem.

    I am using the following cURL command to imitate these requests to POST my
    firmware file over to the computer:
    
        *curl -i -X POST -d @"C:\Users\name\Documents\firmware/firmwarefile"
    

    First, avoid the -X:

    https://daniel.haxx.se/blog/2015/09/11/unnecessary-use-of-curl-x/

    Then, as you want multipart formposts you want -F and not -d:

    https://ec.haxx.se/http-postvspost.html

    Am I posting my file correctly? I don't understand why the firmware file is not being pushed through.

    I'd like to refer to the "HTTP multipart formposts" section of the curl book first and if there's anything unclear after you've read that, please ask specify and I'll try to elaborate in a response or clarify the book chapter...

    https://ec.haxx.se/http-multipart.html

    --

    / daniel.haxx.se

    This is my final cURL command, in which successfully submits the file.

    curl -i -F image=@"C:\Users\user\Documents\Firmware\firmwareFile" http://192.168.243.179:8080/firmware/file