views:

28

answers:

1

How do I specify the content type of a specific part of a multipart/form-data request? The content type for the image is being sent as application/octet-stream, however the server is expecting it to be image/jpeg. This causes the server to deny my request.

$data["file"] = "@/image.jpg";
$data["title"] = "The title";
$data["description"] = "The description";

//make the POST request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);

This is the relevant parts of the request:

Content-Type: multipart/form-data; boundary=----------------------------fc57f743c490

------------------------------fc57f743c490
Content-Disposition: form-data; name="file"; filename="NASA-23.jpg"
Content-Type: application/octet-stream

I want it to be:

Content-Type: multipart/form-data; boundary=----------------------------fc57f743c490

------------------------------fc57f743c490
Content-Disposition: form-data; name="file"; filename="NASA-23.jpg"
Content-Type: image/jpeg
A: 

You would do something like this,

$data["file"] = "@/image.jpg;type=image/jpeg";

//make the POST request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);
ZZ Coder