我正在尝试将multipart/form-data
表单发送到REST API。应该可以发送文件,但其他POST字段应该以{{1}}传递。
它应该是这样的:
JSON
我正在尝试使用PHP ---------------------------acebdf13572468
Content-Disposition: form-data; name=entity
Content-Type: application/json
{
"vacancyId": "xxx",
"locationId": xxx,
"salutationId": xxx,
"firstname": "xxx",
"lastname": "xxx",
"xingProfile": "xxx",
"mobileNumber": "xxx",
"emailAddress": "xxx",
"comment": "xxx",
"mediaOptionId": xxx
}
---------------------------acebdf13572468
Content-Disposition: form-data; name="file"; filename="Archive.zip"
Content-Type: application/x-zip-compressed
<@INCLUDE *xxx\Archive.zip*@>
---------------------------acebdf13572468
Content-Disposition: form-data; name="file"; filename="CV.pdf"
Content-Type: application/octet-stream
<@INCLUDE *xxx\CV.pdf*
---------------------------acebdf13572468--
解决此问题。
这是它的当前代码:
curl
该请求对我来说没问题,但由于某种原因,API无法解析JSON。我很确定这是因为$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: ' . $contentType));
curl_setopt($curl, CURLOPT_POST, 1);
// ...
$data = json_decode($data, true); // data comes as JSON string
$tmpData = $data;
$tmpData['file'] = null; // remove files
$tmpData = (object) array_filter((array) $tmpData); // filter empty values
$json = json_encode($tmpData); // generate JSON entity
// generate data for posting
$postData = array();
$postData['entity'] = 'Content-Type: application/json\r\n\r\n' . $json;
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type' => 'multipart/form-data'));
// go through uploaded files
for ($i = 0; $i < count($data['file']); $i++) {
$file = $data['file'][$i];
$uploadedFile = curl_file_create($file['tmp_name'], $file['type'], $file['name']);
$postData['file'] = $uploadedFile;
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
,这不是这样的。但也许这是另一个问题。有任何想法吗?
这是API获取的请求:
Content-Type: application/json
PS:暂时可以忽略文件。这似乎是JSON部分的一个问题。