Upload Attachment to ALM REST API using multiplepart/form-data header with C# code (675 Views)
Reply
Occasional Collector
pcpaukm
Posts: 1
Registered: ‎12-30-2013
Message 1 of 2 (675 Views)

Upload Attachment to ALM REST API using multiplepart/form-data header with C# code

From the documentation to upload attachment with content-type: multipart/form-data header, I know the following is needed:

 

Header:
Content-Type: multipart/form-data; boundary=exampleboundary;

 

Data:

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

example.txt

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

[Content of file]
--exampleboundary--

 

I wrote the following test code:

public HttpResponseMessage UploadFile(HttpRequestMessage request, Stream stream, string fileName)
        {
            using (HttpClient httpClient = new HttpClient {BaseAddress = new Uri("MY URL")})
            {
                using (var content = new MultipartFormDataContent())
                {
                    var mutipleFormData1 = new StringContent(fileName);
                    mutipleFormData1.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                    {
                        Name = "\"filename\"",
                    };
                    mutipleFormData1.Headers.ContentType = null;
 
                    content.Add(mutipleFormData1);
 
                    var mutipleFormData2 = new StreamContent(stream);
                    mutipleFormData2.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                    {
                        Name = "\"file\"",
                        FileName = "\"" + fileName + "\""
                    };
                    mutipleFormData2.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
                    content.Add(mutipleFormData2);
 
                    request.Content = content;
                    return httpClient.PostAsync(request.RequestUri, content).Result;
                }
            }
        }

 

 

In fiddler, I could see it populated properly:

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

 

But the response from the REST API returns:

HP Application Lifecycle Management
String index out of range: -1
Exception Id: qccore.general-error 
Show Details
Exception Properties:
 
Stack Trace:
 

 

I tested with a 3rd party browser-based REST client sending the same request and it works prefectly, could someone tell me what is wrong with my C# code?

Occasional Visitor
Rembaldi
Posts: 1
Registered: ‎09-09-2014
Message 2 of 2 (204 Views)

Re: Upload Attachment to ALM REST API using multiplepart/form-data header with C# code

Did you ever figure this out?  I have the same issue but the attachment appears to get added.

 

<QCRestException><Id>qccore.general-error</Id><Title>String index out of range: -1</Title><StackTrace>java.lang.StringIndexOutOfBoundsException: String index out of range: -1&#xD;
at java.lang.String.substring(String.java:1937)&#xD;
at org.apache.wink.common.internal.providers.multipart.MultiPartParser.parseHeaders

 

From fiddler:

 

POST http://tlvqc:8080/qcbin/rest/domains/WAS/projects/IMPACT360/defects/130450/attachments HTTP/1.1
Content-Type: multipart/form-data; boundary=--8d19a63e4b94f27
Host: tlvqc:8080
Content-Length: 678
Expect: 100-continue

 

----8d19a63e4b94f27
Content-Disposition: form-data; name="filename"

1402019845_status.png

 

----8d19a63e4b94f27
Content-Disposition: form-data; name="file"; filename="1402019845_status.png"
Content-Type: image/png

 

..(continued)

 

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.