I’m trying to test a rest api for upload a local file to a https url. I am currently using curl to do it. The problem is that my POST in curl is getting converted into PUT (http://curl.haxx.se/docs/manpage.html) mentions the same.
To test from command line I’m using the following:
curl -H “Content-Type: application/json” -H “Authorization:Bearer XXXXXXXXXXXXXXXYYYYYzzzzzz” -H “Accept-Version: ~1” -H “Except: 100-continue” -H “Accept-Version: ~1 ” -T “somefile.pdf” “https://abc-xyz.co/docs”
Output:
{"code":"BadMethod","message":"/docs does not support PUT"}
my php code:
$url = '"https://abc-xyz.co/docs';
$filepath = '/Users/me/Documents/somefile.pdf'; //mac path
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST" );
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Authorization: Bearer XXXXXXXXXXXXXXXYYYYYzzzzzz', 'Accept-Version: ~1', 'Except: 100-continue');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$file = "@".$filepath;
curl_setopt($ch, CURLOPT_POSTFIELDS,$file);
$response = curl_exec($ch);
if(!CURL_ERROR($ch))
{
echo"\n The output is:".$response;
}
else
{
echo "\nUpload failed ".curl_error($ch);
exit;
}
curl_close($ch);
Output: I get parse error.
Can some one educate me with 3 things:
- Can I use curl to do POST for https? if yes, how? and if no what should my approach be?
- How to pass the file path name in my php? I tried “@”.$filepath too but it still doesn’t like it.
- If I want to restrict my file upload type to be only pdf should i be using mime type? (application/pdf????)
Thanks,
DR
From the commandline, see post fileupload with curl.
From PHP, see the manual