QC REST API:不支持的媒体类型 [英] QC REST API : Unsupported Media Type

查看:30
本文介绍了QC REST API:不支持的媒体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用 HP Quality Center 并将 11.5x 升级到 12.21,我使用 API 创建票证.连接和工单创建可以,但文件附件不行.

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.

我得到了 {"Id":"qccore.general-error","Title":"Unsupported Media Type","ExceptionProperties":null,"StackTrace":null}

这是我的代码

$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);

如果我使用其他多部分/表单数据,我会得到 {"Id":"qccore.general-error","Title":"非法的多部分参数.未创建附件.","ExceptionProperties":null,"StackTrace":null}

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.

感谢您的帮助,

推荐答案

我们使用稍微不同的端点.我注意到,尽管许多旧"端点仍然可以在 HP ALM 12.x 中运行,但其中一些端点的行为发生了很大变化,而且大多数端点都有替代品,应该使用这些替代品.

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.

我们使用/rest/domains/DOMAIN/projects/PROJECT/defects/4711/attachments(而不是 /api/... 并通过表单字段传递实体 ID).

We use /rest/domains/DOMAIN/projects/PROJECT/defects/4711/attachments (instead of /api/... and passing entity ID via form fields).

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

Then, we add an HTTP Header Slug: myfilename.txt

我们的其他 HTTP 标头是:

Our other HTTP Headers are:

Accept: application/xml
Content-Type: application/octet-stream

现在,我们只将文件数据 POST 到该端点.这适用于 HP ALM 12.50.

Now, we only POST the file data to that endpoint. That works fine with HP ALM 12.50.

另见http://stevenckwong.com/wp/2016/05/09/how-to-upload-an-attachment-to-a-test-in-alm-via-the-rest-api/ 类似的例子(也是 Java 代码,抱歉).

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).

这篇关于QC REST API:不支持的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆