locked
API2 client.PostAsync - post several records at once or 1-by-1 only? RRS feed

  • Question

  • User-1877975950 posted

    Hi all,

    is it possible to use API2 client.PostAsync to post several records from a json serialised object?

    or have I got to go round in a loop and post  1 record at a time?

    something like this (not working):

    var serialisedMeasureList = JsonConvert.SerializeObject(recsforTR_IDList, Formatting.Indented);

    var client = new HttpClient();

    var url = string.Format("{0}/api/MEASURE_CAPTURE1/PutByTR_ID", APIConnection);

    var response = client.PostAsync(url, serialisedMeasureList).Result;

    many thanks

    Stew

    Friday, February 12, 2016 11:21 AM

Answers

  • User-782957977 posted

    PostAsJsonAsync is more user friendly if you are posting json data and we can post with less code. Only thing is your Web Api service should support Json media formatter.

    If you want to have more control over Json serilization or you want to use custom media formatter, you can use PostAsync().

    You can use PostAsync as given below

    	   string jsonString = JsonConvert.SerializeObject(productList);
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://localhost:58745/");
                    var response = client.PostAsync("Api/Values",
            new StringContent(jsonString,
                Encoding.UTF8, "application/json"))
                .Result;
    
                    if (response.IsSuccessStatusCode)
                    {
                        string responseString = response.Content.ReadAsStringAsync().Result;
                    }
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 13, 2016 10:02 PM

All replies

  • User-782957977 posted

    Please do like this

    var productList = new List<Product>();
                productList.Add(new Product() { Id = 1, Name = "P1", Price=10});
                productList.Add(new Product() { Id = 2, Name = "P2", Price = 20 });
                productList.Add(new Product() { Id = 3, Name = "P3", Price = 10 });
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://localhost:58745/");
                    var response = client.PostAsJsonAsync("Api/Values", productList).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        string responseString = response.Content.ReadAsStringAsync().Result;
                    }
                }

    Web Api Post method

            public void Post([FromBody]List<Product> productList)
            {
                
            }

    Friday, February 12, 2016 1:06 PM
  • User-1877975950 posted

    Hi Santhoshje,

    many thanks for your reply.  var response = client.PostAsJsonAsync("Api/Values", productList).Result;
    I read previously to use 'PostAsJsonAsync',  by adding an extension namespace.

    It is suggested by various posts to use PostAsync (for reasons not given here).

    When I use PostAsync I get error messages saying the list or model (several records) data cannot be converted to string.

    If you could please post your thoughts on this then that would be fab.

    thanks

    Stew

    Friday, February 12, 2016 3:22 PM
  • User-782957977 posted

    PostAsJsonAsync is more user friendly if you are posting json data and we can post with less code. Only thing is your Web Api service should support Json media formatter.

    If you want to have more control over Json serilization or you want to use custom media formatter, you can use PostAsync().

    You can use PostAsync as given below

    	   string jsonString = JsonConvert.SerializeObject(productList);
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://localhost:58745/");
                    var response = client.PostAsync("Api/Values",
            new StringContent(jsonString,
                Encoding.UTF8, "application/json"))
                .Result;
    
                    if (response.IsSuccessStatusCode)
                    {
                        string responseString = response.Content.ReadAsStringAsync().Result;
                    }
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 13, 2016 10:02 PM
  • User36583972 posted

    Hi stewa11,

    or have I got to go round in a loop and post  1 record at a time?

    I suggest you can use batch support and asynchronous methods to achieve your needs.

    More detailed information you can refer to the following posts:

    http://forums.asp.net/t/2085137.aspx?API2+MVC+Controller+PUT+with+several+database+records+in+a+list+How+to+call+

    Best Regards,

    Yohann Lu

    Monday, February 22, 2016 12:53 PM