0

I have an endpoint request me to send an array as part of my body data and below is the code I have using multipart form-data. How do send the array to my endpoint.

var file = _mediaFile.Path;

if (string.IsNullOrEmpty(file) == false)
{
    var upfilebytes = System.IO.File.ReadAllBytes(file);

    MultipartFormDataContent content = new MultipartFormDataContent();
    ByteArrayContent baContent = new ByteArrayContent(upfilebytes);
    var name = System.IO.Path.GetFileName(file);

    baContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/" + System.IO.Path.GetExtension(name).Remove(0, 1));
    baContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
                {
                    Name = "Image",
                    FileName = name,
                };

    var categorynew = new List<string>();
    categorynew.Add(((Categorypicker.SelectedItem as CategoriesModel).category));

    var categoryArray = categorynew.ToArray();

    content.Add(baContent, "Image", name);
    content.Add(new StringContent(DealerAddr.Text), "DealersAddress");
    content.Add(new StringContent(itemName.Text), "ItemName");
    content.Add(new StringContent(itemDescription.Text), "ItemDescription");
    content.Add(new StringContent(itemPrice.Text), "PreferredPrice");
    content.Add(new StringContent(DealerPhone.Text), "DealerPhone");
    content.Add(new StringContent(DealerCity.Text), "City");
    content.Add(new StringContent(StoreUrl.Text), "SellerWeblink");
    content.Add(new StringContent (categorynew,"Category_Name");
    content.Add(new StringContent((Categorypicker.SelectedItem as CategoriesModel).category), "Category_Name");

    var response = await client.PostAsync(CreateItem, content);

    if (response.IsSuccessStatusCode) 
    {
    }
}
1
  • Maybe this can be helpful . Commented Nov 29, 2019 at 8:16

2 Answers 2

3

Okay I later solved this by converting my array back to json before adding it to my content..

var categorynew = new List<string>();
categorynew.Add(((Categorypicker.SelectedItem as CategoriesModel).category));

var categoryArray = categorynew.ToArray();
var jsoncategoryArray = JsonConvert.SerializeObject(categoryArray);

content.Add(baContent, "Image", name);
content.Add(new StringContent(DealerAddr.Text), "DealersAddress");
content.Add(new StringContent(itemName.Text), "ItemName");
content.Add(new StringContent(itemDescription.Text), "ItemDescription");
content.Add(new StringContent(itemPrice.Text), "PreferredPrice");
content.Add(new StringContent(DealerPhone.Text), "DealerPhone");
content.Add(new StringContent(DealerCity.Text), "City");
content.Add(new StringContent(StoreUrl.Text), "SellerWeblink");
content.Add(new StringContent(jsoncategoryArray),"category");
Sign up to request clarification or add additional context in comments.

1 Comment

Glad to hear that . Thanks for sharing the answer , remember to mark it later when have time :)
0

There's, to me, a better solution, that does not need any data content manipulation, let's say we're waitting the array in a parameter named "Categories" :

foreach(var cat in categories)
                mfd.Add(new StringContent(arg, Encoding.UTF8), "Categories[]");

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.