【问题标题】:QC REST API : Unsupported Media TypeQC REST API:不支持的媒体类型
【发布时间】:2016-07-14 08:56:24
【问题描述】:

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

我收到了{"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":"Illegal multi-part arguments. Attachment wasn't created.","ExceptionProperties":null,"StackTrace":null}

所以我有一个多部分结构错误或内容类型的错误标头,但我们所有的测试都失败了。

我们尝试通过八位字节流方法放置附件,但出现媒体类型错误。

感谢您的帮助,

【问题讨论】:

    标签: api rest attachment multipart hp-quality-center


    【解决方案1】:

    我们使用稍微不同的端点。我注意到,尽管许多“旧”端点在 HP ALM 12.x 中仍然有效,但其中一些改变了很多行为,并且其中大多数都有应该使用的替代品。

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

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

    我们的其他 HTTP 标头是:

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

    现在,我们只将文件数据 POST 到该端点。这适用于 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 代码,抱歉)。

    【讨论】:

      猜你喜欢
      • 2021-12-21
      • 2015-06-02
      • 1970-01-01
      • 2018-06-01
      • 2015-10-25
      • 2020-10-06
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      相关资源
      最近更新 更多