2016-07-14 37 views
1

我們使用HP Quality Center,並將11.5x升級到12.21,並使用API​​創建票證。 Connexion和票據創建可以,但文件的附件不是。QC REST 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); 

如果我用其他的multipart/form-data的我得到{"Id":"qccore.general-error","Title":"Illegal multi-part arguments. Attachment wasn't created.","ExceptionProperties":null,"StackTrace":null}

所以我有一個多結構錯誤或壞頭對於內容類型,但我們所有測試都失敗。

我們嘗試通過八位字節流方法來放置附件,但卻得到了媒體類型錯誤。

感謝您的幫助,

回答

0

我們使用稍微不同的終點。我注意到,雖然許多「舊」端點仍然在HP ALM 12.x中運行,但其中一些端點改變了其行爲,並且大部分端點都應該使用替代方法。

我們使用 /rest/domains/DOMAIN/projects/PROJECT/defects/4711/attachments (而不是/api/...和經由表單字段傳遞實體ID)。

然後,我們添加一個HTTP頭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代碼,很抱歉)。

相關問題