Specifying Content-Type of each part of multipart / form-data in PHP curl

How to specify the content type of a specific part of the multipart / form-data request? The content type for the image is sent as application/octet-stream , however the server expects it to be image/jpeg . This causes the server to reject 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); 

These are 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 this to be:

 Content-Type: multipart/form-data; boundary=----------------------------fc57f743c490 ------------------------------fc57f743c490 Content-Disposition: form-data; name="file"; filename="NASA-23.jpg" Content-Type: image/jpeg 
+4
source share
1 answer

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); 
+5
source

Source: https://habr.com/ru/post/1315144/


All Articles