88

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"

4 Answers 4

209

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
Sign up to request clarification or add additional context in comments.

4 Comments

@user381091 Depends on your platform, but it seems many are supported. Here is a link to the download wizard on the official curl website: curl.haxx.se/dlwiz/?type=bin
Note that curl's --data 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 instead.
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 '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. stackoverflow.com/questions/71387634/… .
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"
29
curl -X PUT -T "/path/to/file" "http://myputserver.com/puturl.tmp"

2 Comments

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.
-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).
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);

Comments

0

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

1 Comment

-d (or --data) is inappropriate for generic data upload. See documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.