Post data to API and capture the response

Ankaprasad
Posted by Ankaprasad under C# category on | Points: 40 | Views : 2255
  public static void CallPostAPI()
{

LeadTest obj = new LeadTest() { FName="External2", LName="Test", FullName ="Test", Mobile ="9876530", Email = "External2@gmail.com", City ="Bangalore", State =1,
Country="India", ZipCode="12345",LeadSource=8,RentType=1,PolicyInputPropertyType=1,NumberOfFamily=1,
Product= "TestProduct"
};

string jsondata= JsonConvert.SerializeObject(obj);

var content = new StringContent(jsondata, Encoding.UTF8, "application/json");//after
var client2 = new HttpClient { BaseAddress = new Uri("http://localhost:63584/") };
//client2.DefaultRequestHeaders.Add("Authorization", value);

var response = client2.PostAsync("api/<ControllerName>/GenerateLead", content).Result;

using (HttpContent data = response.Content)
{
// ... Read the string.
var result = data.ReadAsStringAsync();
string s = result.Result;
}


}
}

Comments or Responses

Login to post response