Send List of data to web api using httppost from c#

      public Result SaleListSend(string _baseUrl, Token token, List<SaleViewModel> list)
        {
            var client = new HttpClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken);

            var serializedDate = JsonConvert.SerializeObject(list);

            var content = new StringContent(serializedDate, Encoding.UTF8, "application/json");

            using (var response = client.PostAsync(_baseUrl + "api/Sale/PostSales", content))
            {
                if (response.Result.IsSuccessStatusCode)
                {
                    string stateInfo = response.Result.Content.ReadAsStringAsync().Result;
                    Result = JsonConvert.DeserializeObject<Result>(stateInfo);
                }
                else
                {
                    throw new Exception(response.Result.ReasonPhrase);
                }
            }
            return Result;
        }

Comments

Popular posts from this blog

How to use DbFunctions.TruncateTime

Provision AWS EC2 Instance and RDS with Terraform, and Deploy Spring Boot App to EC2 Instance via GitHub Action Pipeline

Microsoft Access Connectivity from C#