1

From ASP .NET Core MVC I need to send request to API endpoint which requires multipart/form-data with the following format

{boundary value}
Content-Disposition: form-data; name='file'; filename='filename.jpg'
Content-Type: image/jpeg

{file content}
--{boundary value}--

Within controller, I have uploaded the file as IFormFile interface which has properties:

Content-Type
ContentDisposition

How can I construct multipart/form-data and send it?

I've tried to use MultipartFormDataContent which has overloaded constructor with string boundary but without success.

1 Answer 1

1
 public async Task<HttpResponseMessage> PostMultipart(string apiendpoint, byte[] data)
        {

            var multipartContent = new MultipartFormDataContent(); // your boundary value if need anything can be passed in the contructore
            var fileContent = new ByteArrayContent(data);
            fileContent.Headers.ContentType =
                MediaTypeHeaderValue.Parse("image/jpeg");

            multipartContent.Add(fileContent, "file", "filename.jpg");
            //client is HttpClient static field in the class
            return await client.PostAsync(apiendpoint, multipartContent);
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.