2017-11-15 14 views
0

ASP.Net 프로젝트에서 작업 중입니다. HP QC ALM 12.5x의 결함에 첨부 파일을 업로드해야합니다.C# HP QC ALM REST API : 결함 첨부 파일 업로드

인증 부분이 이미 완료되어 성공적으로 작동합니다. 나는 아래의 코드

string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); 
byte[] boundaryBytes = System.Text.Encoding.ASCII.GetBytes("--" + boundary + "\r\n"); 

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(HPALMUrl + "/rest/domains/" + HPALMDomain + "/projects/" + HPALMProject + "/attachments"); 
request.Method = "POST"; 
request.Accept = "application/json"; 
request.Headers.Add("Accept-Language", "en-US"); 
request.Headers.Add("Accept-Encoding", "gzip, deflate, sdch"); 
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"; 
request.ContentType = "Content-Type: multipart/form-data; boundary=" + boundary; 
request.AllowWriteStreamBuffering = false; 
request.ContentLength = imageBytes.Length; 
authRequest.KeepAlive = true; 
request.CookieContainer = createSessionRequest.CookieContainer; //successfully authenticated cookie 

string contentDisposition = "form-data; entity.type=\"{0}\"; entity.id=\"{1}\"; filename=\"{2}\"; Content-Type=\"{3}\""; 
request.Headers.Add("Content-Disposition", string.Format(contentDisposition, "defect", "4", "Defect Attachment.png", "application/octet-stream")); //I am not clear with passing content disposition. Please let me know if this should be modified. 

내가

string path = @"C:\TestAttachment.png"; 
byte[] imageBytes = System.IO.File.ReadAllBytes(path); 
using (Stream requestStream = request.GetRequestStream()) 
{ 
    requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); 
    using (MemoryStream memoryStream = new MemoryStream(imageBytes)) 
    { 
     byte[] buffer = new byte[imageBytes.Length]; 
     int bytesRead = 0; 
     while ((bytesRead = memoryStream.Read(buffer, 0, buffer.Length)) != 0) 
     { 
      requestStream.Write(buffer, 0, bytesRead); 
     } 
    } 
} 

var response = (HttpWebResponse)request.GetResponse(); //Receiving Error here 

string responseText = string.Empty; 
if (response.StatusCode == HttpStatusCode.OK) 
    responseText = "Update completed"; 
else 
    responseText = "Error in update"; 

은 알려 주시기 바랍니다 아래처럼 이미지 파일을 업로드하려고에게 쓴 WebException was unhandled: The remote server returned an error: (415) Unsupported Media Type.

수신 첨부 파일에 결함을 업로드에

, 내가 뭘 잘못하고 있는지.

또한 rest api에 언급 된 콘텐츠 처리를 전달하는 것이 확실하지 않습니다.

미리 감사드립니다.

+0

'Content-Disposition'은 * response * 헤더입니다. * 서버 *가 응답으로 설정합니다. 전혀 요청하지 마십시오. –

+0

오류 자체는 첨부 파일이 * files *이지만 'Content-type'은'multipart/form-data'입니다. 양식을 게시하지 않았습니다. 새 파일을 작성 중입니다. 아마도 일반적인 HTTP와 REST가 어떤 방식으로 작동하는지 확인해야 할 것입니다. –

+0

@PanagiotisKanavos 그러나 [API 참조] (https://admhelp.microfocus.com/alm/en/latest/api_refs/REST/webframe.htm#attachments.htm)에서 내용 부분은 헤더 부분에 언급되어 있습니다. –

답변

1
def upload_result_file(self, defect_id, report_file): 
    ''' 
     Function : upload_result_file 
     Description : Upload test result to ALM 
    ''' 
    payload = open(report_file, 'rb') 
    headers = {} 
    headers['Content-Type'] = "application/octet-stream" 
    headers['slug'] = "test-results" + report_file[report_file.rfind(".")+1: ] 
    response = self.alm_session.post(ALM_URL + ALM_MIDPOINT + "/defects/" + 
            str(defect_id) + "/attachments/", 
            headers=headers, data=payload) 
    if not (response.status_code == 200 or response.status_code == 201): 
     print "Attachment step failed!", response.text, response.url, response.status_code 
    return 
+0

나는 그것을 만들었다. 당신은 항상 나를 도왔습니다. 귀하의 코드를 보내 주셔서 감사합니다! –