I need to send an HTTP request as a MultiPartFormData to a REST controller. It was working, but now the check I have on my controller is claiming that the request is not of the correct type, even when I can see in the debugger that the request is on the correct type. For reference:
Here's the console app code that is calling it:
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace QuickUploadTestHarness
{
class Program
{
static void Main(string[] args)
{
using (var client = new HttpClient())
using (var content = new MultipartFormDataContent())
{
// Make sure to change API address
client.BaseAddress = new Uri("http://localhost");
// Add first file content
var fileContent1 = new ByteArrayContent(File.ReadAllBytes(@"C:\<filepath>\test.txt"));
fileContent1.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "testData.txt"
};
//Add Second file content
var fileContent2 = new ByteArrayContent(File.ReadAllBytes(@"C:\<filepath>\test.txt"));
fileContent2.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "Sample.txt"
};
content.Add(fileContent1);
content.Add(fileContent2);
// Make a call to Web API
var result = client.PostAsync("/secret/endpoint/relevant/bits/here/", content).Result;
Console.WriteLine(result.StatusCode);
Console.ReadLine();
}
}
}
}
How is it possible that it's being interpreted as not MultiPartFormData? Note the "using MultiPartFormDataContent" for the request
For
MultiPartFormDataContent
you can try to use thecontent.Add
overload that takes aname
andfilename
argument. MSDN MultipartFormDataContent.Add Method (HttpContent, String, String)Greetings