0

I am trying to upload a file to a REST endpoint using .NET C#. I have tried numerous permutations seen here on SO and other forums with no success. The closest I get is a 400 response from the server. I could post the code i've tried, but it's well over 200 lines of garbled mess accumulated over about 8 hours, see links below for what i've tried.

The form below is located at https://domain.com/fileAttachments/formData/

Form HTML:

<form action="https://domain.com/fileAttachments" method="POST" enctype="multipart/form-data">
<!-- File Attachment [0] -->
<p><label>ContentType</label>
<br>
<input name="contentType[0]" value="application/octet-stream" pattern='^[a-zA-Z-]+/[-a-zA-Z0-9.+_*]+$' title='^[a-zA-Z-]+/[-a-zA-Z0-9.+_*]+$'  maxlength="128"  type="text">
</p>
<p><label>Data</label>
<br>
<input name="data[0]" type="file" required>
</p>
<p><label>FileName</label>
<br>
<input name="fileName[0]" pattern='^[^\t\n\/:*?"<>|]*$' title='^[^\t\n\/:*?"<>|]*$'  maxlength="100"  type="text" required>
</p>
<p><label>Description</label>
<br>
<textarea name="description[0]"  maxlength="1333"  rows=6 ></textarea>
</p>
<p><label>Name</label>
<br>
<input name="name[0]" pattern='^[^\t\n]*$' title='^[^\t\n]*$'  maxlength="40"  type="text">
</p>
<br>
<input type="submit" id="submit" value="Upload">
</form>

The POST headers through a browser POST are:

Request Payload
------WebKitFormBoundary8Z24B55DIvnjkUGF
Content-Disposition: form-data; name="contentType[0]"

application/octet-stream
------WebKitFormBoundary8Z24B55DIvnjkUGF
Content-Disposition: form-data; name="data[0]"; filename="file.pdf"
Content-Type: application/pdf


------WebKitFormBoundary8Z24B55DIvnjkUGF
Content-Disposition: form-data; name="fileName[0]"

testFileName
------WebKitFormBoundary8Z24B55DIvnjkUGF
Content-Disposition: form-data; name="description[0]"


------WebKitFormBoundary8Z24B55DIvnjkUGF
Content-Disposition: form-data; name="name[0]"


------WebKitFormBoundary8Z24B55DIvnjkUGF--

Some of the posts I have tried:

How to submit a multipart/form-data HTTP POST request from C# How to fix 400 Bad Request error? Send file+parameters in post request UploadFile with POST values by WebClient How to upload file to server with HTTP POST multipart/form-data c# setting uploadFile content-Type

How do I craft an HTTP POST request so the file is uploaded successfully? Please let me know if you need additional clarification, and thank you in advance.

germoo
  • 16
  • 3

1 Answers1

0

I was able to use WebRequest.Write to POST the data as a byte-array.

private string setData(string address, string serializedData, WebMethods type)
    {
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        byte[] data = Encoding.ASCII.GetBytes(serializedData);
        WebRequest request = WebRequest.Create(address);
        CredentialCache mycreds = new CredentialCache();
        mycreds.Add(new Uri(_baseAddress), "Basic", new NetworkCredential(_username, _password));
        request.Credentials = mycreds;
        request.Method = type.ToString();
        request.ContentLength = data.Length;
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(data, 0, data.Length);
        dataStream.Close();
        WebResponse response = request.GetResponse();
        string responseFromRequest = ((HttpWebResponse)response).StatusDescription;
        response.Close();
        return responseFromRequest;
    }
germoo
  • 16
  • 3