问题
We use HP Quality Center and we upgrade 11.5x to 12.21 and i use the API to create a ticket. Connexion and ticket creation are ok, but attachement of file is not.
I got {"Id":"qccore.general-error","Title":"Unsupported Media Type","ExceptionProperties":null,"StackTrace":null}
Here is my code
$filename = $file->name;
$eol = "\r\n";
$mime_boundary = 'boundary';
$content = '--' . $mime_boundary . $eol;
$content .= 'Content-Disposition: form-data; name="entity.type"';
$content .= $eol . $eol;
$content .= 'defect';
$content .= $eol;
$content = '--' . $mime_boundary . $eol;
$content .= 'Content-Disposition: form-data; name="entity.id"';
$content .= $eol . $eol;
$content .= $id;
$content .= $eol;
$content = '--' . $mime_boundary . $eol;
$content .= 'Content-Disposition: form-data; name="filename"';
$content .= $eol . $eol;
$content .= utf8_encode($filename);
$content .= $eol;
$content .= '--' . $mime_boundary . $eol;
$content .= 'Content-Disposition: form-data; name="file"; filename="' . utf8_encode($filename) . '"';
$content .= $eol;
$content .= 'Content-Type: ' . $file['type'];
$content .= $eol . $eol;
$dt = explode('-', $file->create_dt);
$path_file = $config['files']['forms_attachments_path'] . DIRECTORY_SEPARATOR . $dt[0] . DIRECTORY_SEPARATOR . $dt[1] . DIRECTORY_SEPARATOR . $file->filename;
$handle = fopen($path_file, 'r');
$content .= fread($handle,filesize($path_file));
fclose($handle);
$content .= $eol;
$content .= '--' . $mime_boundary . '--';
$header = array(
'Content-Type: multipart/mixed; boundary='.$mime_boundary,
'Content-Length: '.strlen($content),
'Accept: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_COOKIE, $cookiess);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_PROXY, null);
curl_setopt($ch, CURLOPT_URL, $config['qc']['url'] . '/api/domains/' . $config['qc']['domain']. '/projects/' . $config['qc']['project'] . '/attachments/');
$output = curl_exec($ch);
curl_close($ch);
var_dump($output);
If i use the other multipart/form-data i got {"Id":"qccore.general-error","Title":"Illegal multi-part arguments. Attachment wasn't created.","ExceptionProperties":null,"StackTrace":null}
So i have a multipart structure mistake or a bad header for content-type, but all we test fails.
We try to put attachment by octet stream method but got the media type error.
Thanks for your help,
回答1:
We use a slightly different endpoint. I noticed that, although many "old" endpoints still work in HP ALM 12.x, some of them changed their behaviour a lot, and most of them have replacements which should be used instead.
We use
/rest/domains/DOMAIN/projects/PROJECT/defects/4711/attachments
(instead of /api/...
and passing entity ID via form fields).
Then, we add an HTTP Header Slug: myfilename.txt
Our other HTTP Headers are:
Accept: application/xml
Content-Type: application/octet-stream
Now, we only POST the file data to that endpoint. That works fine with HP ALM 12.50.
Also see http://stevenckwong.com/wp/2016/05/09/how-to-upload-an-attachment-to-a-test-in-alm-via-the-rest-api/ for a similar example (also Java code, sorry).
来源:https://stackoverflow.com/questions/38369755/qc-rest-api-unsupported-media-type