4

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

Eimantas
  • 48,927
  • 17
  • 132
  • 168
Nininea
  • 2,671
  • 6
  • 31
  • 57
  • Did you compare the posts? Show us the messamge which your program sends. You can get it with your fiddler.. – kara Jan 09 '18 at 09:08
  • unfortunately I'm not able to do this. Fiddler is not working well in mac – Nininea Jan 09 '18 at 09:09
  • 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 Jan 09 '18 at 09:16
  • 1
    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 Jan 09 '18 at 09:31
  • Have a look at this: https://stackoverflow.com/questions/3808016/how-do-i-see-the-raw-http-request-that-the-httpwebrequest-class-sends – kara Jan 09 '18 at 09:51
  • Instead of Fiddler you can use Charles on MacOS. – tequila slammer Jan 09 '18 at 10:08
  • @tequilaslammer I tried Charles, it logs simulator action (for example using safari or searching on maps), but not for my xamarin.ios application... I have no idea why. have you ever used it for xamarin apps? – Nininea Jan 09 '18 at 11:36

1 Answers1

3

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

    }
Anish Manchappillil
  • 697
  • 2
  • 10
  • 19