简体   繁体   English

QC REST API:不支持的媒体类型

[英]QC REST API : Unsupported Media Type

We use HP Quality Center and we upgrade 11.5x to 12.21 and i use the API to create a ticket. 我们使用惠普质量中心,我们将11.5倍升级到12.21,并使用API​​创建票证。 Connexion and ticket creation are ok, but attachement of file is not. Connexion和票证创建都可以,但文件的附件不是。

I got {"Id":"qccore.general-error","Title":"Unsupported Media Type","ExceptionProperties":null,"StackTrace":null} 我得到{"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} 如果我使用其他multipart / form-data我得到{"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, 谢谢你的帮助,

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. 我注意到,尽管许多“旧”端点仍然在HP ALM 12.x中工作,但是其中一些端点已经改变了很多行为,并且大多数都有替换应该使用。

We use /rest/domains/DOMAIN/projects/PROJECT/defects/4711/attachments (instead of /api/... and passing entity ID via form fields). 我们使用/rest/domains/DOMAIN/projects/PROJECT/defects/4711/attachments (而不是/api/...并通过表单字段传递实体ID)。

Then, we add an HTTP Header Slug: myfilename.txt 然后,我们添加一个HTTP Header Slug: myfilename.txt

Our other HTTP Headers are: 我们的其他HTTP标头是:

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. 这适用于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). 另请参阅http://stevenckwong.com/wp/2016/05/09/how-to-upload-an-attachment-to-a-test-in-alm-via-the-rest-api/以获取类似示例(也是Java代码,对不起)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM