I'm trying to do a POST with a CSV file to a REST endpoint using RestSharp. However, after some extensive tinkering, I've found that the receiving server is picky about the order of the headers in the request body. I need to ensure that after the multi-part form boundary the first thing that appears is the content-disposition. It actually doesn't even need the Content-Type but RestSharp insists on adding it no matter what I do. So I need to force RestSharp to not add the Content-Type, or add it after the Content-Disposition.
It's doing this:
Content-Type: multipart/form-data; boundary="196f530b-7398-43b5-ab7e-f98bd956425a"
Content-Length: 2476
Expect: 100-continue
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
--196f530b-7398-43b5-ab7e-f98bd956425a
Content-Type: text/csv
Content-Disposition: form-data; name="File"; filename="e54bf33c-28cc-4f1e-9cb1-8d2b37a7906b.csv"
"My","CSV","ETC"
I need it to do this:
Content-Type: multipart/form-data; boundary="196f530b-7398-43b5-ab7e-f98bd956425a"
Content-Length: 2476
Expect: 100-continue
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
--196f530b-7398-43b5-ab7e-f98bd956425a
Content-Disposition: form-data; name="File"; filename="e54bf33c-28cc-4f1e-9cb1-8d2b37a7906b.csv"
Content-Type: text/csv
"My","CSV","ETC"
Notice how the content-disposition comes directly after the form boundary. I don't think I had this issue in an older version of RestSharp, but now I do.
So I had to do it manually to control the order of the headers in the body. Here's the code I used if it helps someone else:
oRequest.AddHeader("Content-Type", "multipart/form-data; boundary=X-FILE-BOUNDARY");
var sbBody = new System.Text.StringBuilder();
sbBody.AppendLine("--X-FILE-BOUNDARY");
sbBody.AppendLine("Content-Disposition: form-data; name=\"File\"; filename=\"" + System.IO.Path.GetFileName(strFileName) + "\"");
sbBody.AppendLine("Content-Type: text/csv");
sbBody.AppendLine();
sbBody.AppendLine(System.IO.File.ReadAllText(strFileName));
sbBody.AppendLine("--X-FILE-BOUNDARY--");
oRequest.AddParameter("multipart/form-data", sbBody.ToString(), ParameterType.RequestBody);