I am working on ASP.Net project. I have to upload attachment to the defect in HP QC ALM 12.5x.
Authentication part is already done and successfully working.
On uploading defect to the attachment, receiving WebException was unhandled: The remote server returned an error: (415) Unsupported Media Type.
I have written the below code
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.
I am trying to upload Image file like below
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";
Please let me know what I am doing wrong.
I am also unsure of passing the content disposition mentioned in rest api.
Thanks in Advance!
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