I'm trying to upload a file to an API with PHP. Additionally to the file i need to provide other parameters. My current code looks like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_PUT, 1);
$post = array(
'file' => '@' . realpath('filename'),
'other_parameter' => ''
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array('Content-Type: multipart/form-data');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $pass);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
Server header response:
HTTP/1.1 422 status code 422
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8
Status: 422 Unprocessable Entity
Vary: Origin
X-Content-Type-Options: nosniff
X-Xss-Protection: 1; mode=block
Content-Length: 106
Connection: keep-alive
Also i get the message that the file field cannot be empty.