81

I've written a service using HTTP PUT method for uploading a file.

Web Browsers don't support PUT so I need a method for testing. It works great as a POST hitting it from a browser.

update: This is what worked. I tried Poster but it suffers from the same thing as using fiddler. You have to know how to build the request. curl takes care of the problem.

curl -X PUT "localhost:8080/urlstuffhere" -F "file=@filename" -b "JSESSIONID=cookievalue"
Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
Speck
  • 2,259
  • 1
  • 20
  • 29

4 Answers4

194

In my opinion the best tool for such testing is curl. Its --upload-file option uploads a file by PUT, which is exactly what you want (and it can do much more, like modifying HTTP headers, in case you need it):

curl http://myservice --upload-file file.txt
Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
alienhard
  • 14,376
  • 9
  • 37
  • 28
  • 1
    @user381091 Depends on your platform, but it seems many are supported. Here is a link to the download wizard on the official curl website: http://curl.haxx.se/dlwiz/?type=bin – alienhard Feb 28 '11 at 19:28
  • 9
    Note that curl's [`--data`](http://curl.haxx.se/docs/manpage.html) option, originally used by this answer, is inappropriate for file uploads since it strips out newline characters and thus may modify the file. I have replaced it with [`--upload-file`](http://curl.haxx.se/docs/manpage.html#-T) instead. – Mark Amery May 30 '15 at 01:21
  • I have a Put method that accept zip file as parameters. When I test the Put method to upload zip file using the below curl command: curl--insecure 'https://localhost:7098/TestController/ImportFile' --form 'file=@"./Test_1.zip"'. I get the error of "Failed to open/read local data from file/application". I had created the questions for this on stackoverflow but none had answer. https://stackoverflow.com/questions/71387634/test-zip-file-upload-using-http-put-method-using-the-curl . – Sabbu Mar 09 '22 at 16:39
  • 1
    By using -F options solve my issue: "curl --insecure -i -X PUT -H "Content-Type: multipart/form-data" -F "file=@/C:/test/Test_1.zip" localhost:7098/TestController/ImportFile" – Sabbu Mar 09 '22 at 19:11
25
curl -X PUT -T "/path/to/file" "http://myputserver.com/puturl.tmp"
Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
  • 12
    While this code could solve the problem, it is best to add elaboration and explain how it works for people who might not understand this piece of code. – Papershine Apr 09 '18 at 12:36
  • 5
    `-X PUT` is redundant when using `-T` (which is short for `--upload-file`). This is basically the same as the accepted answer (which is a few years older). – Martin Nov 23 '20 at 17:22
1

If you're using PHP you can test your PUT upload using the code below:

#Initiate cURL object
$curl = curl_init();
#Set your URL
curl_setopt($curl, CURLOPT_URL, 'https://local.simbiat.ru');
#Indicate, that you plan to upload a file
curl_setopt($curl, CURLOPT_UPLOAD, true);
#Indicate your protocol
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
#Set flags for transfer
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
#Disable header (optional)
curl_setopt($curl, CURLOPT_HEADER, false);
#Set HTTP method to PUT
curl_setopt($curl, CURLOPT_PUT, 1);
#Indicate the file you want to upload
curl_setopt($curl, CURLOPT_INFILE, fopen('path_to_file', 'rb'));
#Indicate the size of the file (it does not look like this is mandatory, though)
curl_setopt($curl, CURLOPT_INFILESIZE, filesize('path_to_file'));
#Only use below option on TEST environment if you have a self-signed certificate!!! On production this can cause security issues
#curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
#Execute
curl_exec($curl);
Simbiat
  • 339
  • 2
  • 12
0

For curl, how about using the -d switch? Like: curl -X PUT "localhost:8080/urlstuffhere" -d "@filename"?

ibic
  • 608
  • 1
  • 11
  • 16