4
votes

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

1
Did you compare the posts? Show us the messamge which your program sends. You can get it with your fiddler..kara
unfortunately I'm not able to do this. Fiddler is not working well in macNininea
Then i've to guess: You set the content.Header.ContentType to application/json after this you override it with image/jpeg. I think you're trying to add two headers "content-type" which is not allowed. What did you do in fiddler? could you add two content-types?kara
You're going to have to find out what you are actually sending. If you don't have control over the server, and can't set up fiddler on your client, you'll need to do something like creating a simple ASP.NET project for accepting a form post, and point your client at that.GrandOpener

1 Answers

3
votes

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"];

    }