C# – Httpclient multipart/form-data post image and json same time

chttpclientmultipartform-dataxamarin

I'm trying to upload image and json in one request using c# code, but server always returns 400- bad request. Executing same request using fiddler returns status code 200. help…

Here is my fiddler code :

——WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="application/json" Content-Type: application/json

{"type": "Personal","comments": ["Lorem", "Ipsum" ] }
——WebKitFormBoundary7MA4YWxkTrZu0gW– Content-Disposition: form-data; name="fieldNameHere"; filename="1111.jpg"

Content-Type: image/jpeg

<@INCLUDE C:\Users\user\Desktop\New folder\1111.jpg@>

And implementation in c#:

var boundary = "Upload----" + DateTime.Now.Ticks.ToString();
MultipartFormDataContent form = new MultipartFormDataContent(boundary);
StringContent content = new StringContent(bodyJson);
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
form.Add(content, "application/json");

var imageContent = new ByteArrayContent(image);
imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
form.Add(imageContent, "image/jpeg", "image.jpg");
var responseTask = _httpClient.PostAsync(url, form).Result;

the response is always same :

enter image description here

Best Answer

You can pass the parameter as a string content , check the below sample.

public async Task<JObject> ExecutePostAsync(Stream myStreem, string url, string token, string parameter1, string parameter2, string parameter3)
    {
        try
        {
            using (var content = new MultipartFormDataContent("----MyBoundary"))
            {

                using (var memoryStream = myStreem)
                {
                    using (var stream = new StreamContent(memoryStream))
                    {
                        content.Add(stream, "file", Guid.NewGuid().ToString() + ".jpg");
                        content.Add(new StringContent(parameter1), "parameter1");
                        content.Add(new StringContent(parameter3), "parameter2");
                        content.Add(new StringContent(parameter3), "parameter3");

                        using (HttpClient client = new HttpClient())
                        {
                            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
                            var responce = await client.PostAsync(url, content);
                            string contents = await responce.Content.ReadAsStringAsync();
                            return (JObject.Parse(contents));
                        }

                    }
                }
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

In API get the data from FORM request

    public async Task<IHttpActionResult> UploadFile()
    {

        string parameter1 = HttpContext.Current.Request.Form["parameter1"];
        string parameter2 = HttpContext.Current.Request.Form["parameter2"];
        string parameter3 = HttpContext.Current.Request.Form["parameter3"];

    }
Related Topic