Post File and form data to form c#
<p>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.</p>

<p>The form below is located at <a href="https://domain.com/fileAttachments/formData/" rel="nofollow noreferrer">https://domain.com/fileAttachments/formData/</a></p>

<p>Form HTML:</p>

<pre><code>&lt;form action="https://domain.com/fileAttachments" method="POST" enctype="multipart/form-data"&gt;
&lt;!-- File Attachment [0] --&gt;
&lt;p&gt;&lt;label&gt;ContentType&lt;/label&gt;
&lt;br&gt;
&lt;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"&gt;
&lt;/p&gt;
&lt;p&gt;&lt;label&gt;Data&lt;/label&gt;
&lt;br&gt;
&lt;input name="data[0]" type="file" required&gt;
&lt;/p&gt;
&lt;p&gt;&lt;label&gt;FileName&lt;/label&gt;
&lt;br&gt;
&lt;input name="fileName[0]" pattern='^[^\t\n\/:*?"&lt;&gt;|]*$' title='^[^\t\n\/:*?"&lt;&gt;|]*$'  maxlength="100"  type="text" required&gt;
&lt;/p&gt;
&lt;p&gt;&lt;label&gt;Description&lt;/label&gt;
&lt;br&gt;
&lt;textarea name="description[0]"  maxlength="1333"  rows=6 &gt;&lt;/textarea&gt;
&lt;/p&gt;
&lt;p&gt;&lt;label&gt;Name&lt;/label&gt;
&lt;br&gt;
&lt;input name="name[0]" pattern='^[^\t\n]*$' title='^[^\t\n]*$'  maxlength="40"  type="text"&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;input type="submit" id="submit" value="Upload"&gt;
&lt;/form&gt;
</code></pre>

<p>The POST headers through a browser POST are:</p>

<pre><code>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--
</code></pre>

<p>Some of the posts I have tried:</p>

<p><a href="https://stackoverflow.com/questions/1203811/how-to-submit-a-multipart-form-data-http-post-request-from-c-sharp">How to submit a multipart/form-data HTTP POST request from C#</a>
<a href="https://stackoverflow.com/questions/10131423/how-to-fix-400-bad-request-error">How to fix 400 Bad Request error?</a>
<a href="https://stackoverflow.com/questions/18535799/send-fileparameters-in-post-request">Send file+parameters in post request</a>
<a href="https://stackoverflow.com/questions/11048258/uploadfile-with-post-values-by-webclient">UploadFile with POST values by WebClient</a>
<a href="https://stackoverflow.com/questions/19954287/how-to-upload-file-to-server-with-http-post-multipart-form-data">How to upload file to server with HTTP POST multipart/form-data</a>
<a href="https://stackoverflow.com/questions/15995580/c-sharp-setting-uploadfile-content-type">c# setting uploadFile content-Type</a></p>

<p>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.</p>