I am unable to upload image and data to web service as multipart content.Here is my code
var fileUploadUrl = @"http://myurl";
var client = new HttpClient();
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "multipart/form-data");
photoStream.Position = 0;
// This is the postdata
MultipartFormDataContent content = new MultipartFormDataContent();
content.Add(new StreamContent(photoStream), "attendeedImage");
content.Add(new StringContent("12", Encoding.UTF8), "userId");
content.Add(new StringContent("78", Encoding.UTF8), "noOfAttendees");
content.Add(new StringContent("chennai", Encoding.UTF8), "locationName");
content.Add(new StringContent("32.56", Encoding.UTF8), "longitude");
content.Add(new StringContent("32.56", Encoding.UTF8), "latitude");
Console.Write(content);
// upload the file sending the form info and ensure a result.
// it will throw an exception if the service doesn't return a valid successful status code
await client.PostAsync(fileUploadUrl, content)
.ContinueWith((postTask) =>
{
postTask.Result.EnsureSuccessStatusCode();
});
The response I get is 400- Bad request
Is is possible to send both image and data together like this? If yes, what is the proper way to go about?
I can't test this right now, but the problem could be a missing boundary between your data items. To specify one, initializting your
MultipartFormDataContent
as follows:More about boundaries: What is the boundary in multipart/form-data?