I'm using RestSharp's AddFile and it's working close to fine except my files end up getting broken due to this header information that's being added.
-------------------------------28947758029299
Content-Disposition: form-data; name="user.png"; filename="user.png"
Content-Type: image/png
This was just a test image I uploaded. If I remove these lines from the file then it opens fine, otherwise it seems to be corrupt. Is it possible for me to use AddFile without this stuff getting added?
Current code:
string contentType = MimeMapping.GetMimeMapping("~/uploads/" + filename); //image/png etc
request.AddFile(filename, Server.MapPath("~") + "\\uploads\\" + filename, contentType);
IRestResponse response = client.Execute(request);
Also tried this with the same result:
request.AddHeader("Content-Type", contentType);
byte[] bytes = File.ReadAllBytes(Server.MapPath("~") + "\\uploads\\" + filename);
request.AddBody(new {myFile = File.ReadAllBytes(Server.MapPath("~") + "\\uploads\\" + filename) });
Also this (no files went through at all here): Edit: this worked actually
string contentType = MimeMapping.GetMimeMapping("~/uploads/" + filename);
byte[] bytes = File.ReadAllBytes(Server.MapPath("~") + "\\uploads\\" + filename);
request.AddHeader("Content-Type", contentType);
request.AddParameter(contentType, bytes, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
RestSharp is by default sending the files using multi-part form data and the zendesk api you are using (assuming it's this one https://developer.zendesk.com/rest_api/docs/core/attachments#upload-files) isn't expecting that, so it is also writing the multi-part boundary identifiers from the content into the uploaded file.
This answer https://stackoverflow.com/a/27994251/772973 on this other thread should resolve your problem.
Update
I just put together a console app with the following code in to upload a pdf to an ASP.NET WebApi project that I created as I don't have access to the ZenDesk API
Main in program.cs:
Code in ValuesController.cs
This uploaded the file, it was identical to the original file and the content-type header was set to
application/pdf
and notmultipart/form-data; boundary=-----------------------------28947758029299
Update 2
Added the actual code for Main in program.cs