I don’t have experience with REST APIs. I inherited a .NET 4.7.1 C# client/server and need to add (large) binary file transfer capability.
On the client side, this is how far I’ve gotten:
HttpClient client = new HttpClient();
...
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(?); // TODO: What should I enter here?
using (var content = new MultipartFormDataContent())
{
var fileContent = new ByteArrayContent(bytes);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "My.dat"
};
content.Add(fileContent);
var response = await client.PostAsync(url, content);
response.EnsureSuccessStatusCode();
return response.Headers.Location;
}
Post fails with:
Status Code: 500, ReasonPhrase: 'could not find expected boundary'
I’m not married to the above implementation (found on the web). I can’t use RestSharp, though.
On the server side, how do I extract the bytes from the request parameter?
public async Task ReceiveFile(
HttpListenerRequest request,
...)
{
...
if (request.HasEntityBody)
{
// TODO: Save bytes to a file
}
...
}
I am limited to a handler that gets an HttpListenerRequest parameter.