Eduardo Pacheco
Eduardo Pacheco

Reputation: 21

How upload attachments in HP ALM via RESTful

Anyone knows how to upload an attachment in HP ALM via RESTful? Currently I am writing my request Header/Body in this form:

Dim request As WebRequest = WebRequest.Create("https://[server].saas.hp.com/qcbin/rest/domains/[DOMAIN_NAME]/projects/[PROJECT_NAME]/runs/[ID_EXECUTION]/attachments/")

request4.Method = "Post"

Dim fileToSend As Byte() = File.ReadAllBytes("C:\Users\ECELESTE\Desktop\Teste.txt")
Dim preAttachment As String = "Content-Disposition" + ": " + "form-data; filename=""Test.txt"""

request.ContentType = "multipart/form-data; boundary=boundary"

Using requestStream As Stream = request.GetRequestStream()
    Dim preAttachmentBytes As Byte() = UnicodeEncoding.UTF8.GetBytes(preAttachment)
    requestStream.Write(preAttachmentBytes, 0, preAttachmentBytes.Length)
    requestStream.Write(fileToSend, 0, fileToSend.Length)
End Using

Dim webResponse As WebResponse = request.GetResponse()

But this code return an error (500 - Internal Server Error).

Other Informations: HP ALM version 12.01/Code language VB.NET

Thank you!

Upvotes: 0

Views: 1306

Answers (1)

Allen
Allen

Reputation: 21

It seems a required form field is missing, the "filename".

Also please use the value of "boundary" specified in the "ContentType" to split each form field.

Above all, the "requestStream" in your code should write below content:

--boundary
Content-Disposition: form-data; name="filename"

Test.txt
--boundary
Content-Disposition: form-data; name="file"; filename="Test.txt"
Content-Type: text/plain

[Content of file, your "fileToSend"]
--boundary--

The filename in the "filename" section must be identical to the filename in the "file" section .

Hope it is helpful for you.

Upvotes: 2

Related Questions