using System.Collections.Generic;
using System.Data.SqlClient;
using System.Threading.Tasks;
using System.Net.Http.Headers;
public int CustomerID {get;set;}
public string CustomerName {get;set;}
public string ContactName {get;set;}
public string Address {get;set;}
public string City {get;set;}
public string PostalCode {get;set;}
public string Country {get;set;}
public class ResponseModel
public bool IsSuccessful {get;set;}
public string ResponseText {get;set;}
public static string apiEndpoint = "https://run.mocky.io/v3/7b9d9963-459e-4ec6-b5b5-f45959838357";
public static async Task Main()
var customers = GetCustomers();
await PostToEndpoint(customers);
public static void InsertCustomer()
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
connection.Execute("INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country) VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');");
public static List<Customer> GetCustomers()
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
var customers = connection.Query<Customer>("Select * FROM CUSTOMERS Where CustomerName ='Cardinal'").ToList();
public static string _accessToken = "MySecretToken";
public static async Task PostToEndpoint(List<Customer> model)
using (HttpClient httpClient = new HttpClient())
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken);
var json = JsonConvert.SerializeObject(model);
HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = await httpClient.PostAsync(apiEndpoint, content);
if (response.IsSuccessStatusCode)
var resp = await response.Content.ReadAsStringAsync();
var responseObject = JsonConvert.DeserializeObject<ResponseModel>(resp);
Console.WriteLine($"Success: {responseObject.IsSuccessful} - {responseObject.ResponseText}");
Console.WriteLine($"Error: {response.StatusCode} - {response.ReasonPhrase}");
catch (HttpRequestException e)
Console.WriteLine($"Request error: {e.Message}");