Test file upload using HTTP PUT method Test file upload using HTTP PUT method curl curl

Test file upload using HTTP PUT method


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


curl -X PUT -T "/path/to/file" "http://myputserver.com/puturl.tmp"


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

#Initiate cURL object$curl = curl_init();#Set your URLcurl_setopt($curl, CURLOPT_URL, 'https://local.simbiat.ru');#Indicate, that you plan to upload a filecurl_setopt($curl, CURLOPT_UPLOAD, true);#Indicate your protocolcurl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);#Set flags for transfercurl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);#Disable header (optional)curl_setopt($curl, CURLOPT_HEADER, false);#Set HTTP method to PUTcurl_setopt($curl, CURLOPT_PUT, 1);#Indicate the file you want to uploadcurl_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);#Executecurl_exec($curl);