Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpRequest.Content.IsMimeMultipartContent() is returning false when it should return true

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:

enter image description here

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

like image 269
Matt Avatar asked Sep 25 '15 00:09

Matt


1 Answers

For MultiPartFormDataContent you can try to use the content.Add overload that takes a name and filename argument. MSDN MultipartFormDataContent.Add Method (HttpContent, String, String)

like image 88
Muraad Nofal Avatar answered Oct 19 '22 18:10

Muraad Nofal