Reputation: 115
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
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