Aaron Lamb
Aaron Lamb

Reputation: 115

Set mime-type of multipart header in php curl without passing a file

Can this be done? I'm seeing examples like

curl_setopt($ch, CURLOPT_POSTFIELDS, array(
  'variable' => '@path/to/file;type=application/json'
));

But I'm unable to set this mime-type if I'm passing a string as the variable value. The API with which I'm interfacing is explicitly requiring that this mime-type be set to application/json, but I can't seem to set it with the curl library unless I use a file. For example:

curl_setopt($ch, CURLOPT_POSTFIELDS, array(
  'variable' => 'json_string;type=application/json'
));

outputs only a string with type=application/json appended to the end (which makes sense), and something like

curl_setopt($ch, CURLOPT_POSTFIELDS, array(
  'variable' => sprintf('@%s;type=application/json', $json_string)
));

fails to pass anything in to the body to the server, ostensibly because it can't find the file (?).

Any ideas on how to explicitly set this mime-type without writing out temp files or writing my own post requests?

Upvotes: 2

Views: 1711

Answers (1)

lukassteiner
lukassteiner

Reputation: 847

Are you shure, that you want to set the mime-type in the postfield?

You might want to set it in the http-request-header like so:

CURLOPT_HTTPHEADER => array('Content-Type: application/json'),

Upvotes: 1

Related Questions