How to call REST API Post Method using C#
27 September 2022
|
Viewed 1633 times
What does a REST API do?
A REST API (also known as RESTful API) is an application programming interface (API or web API) that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services.
REST stands for Representational State Transfer
What are GET and POST methods?
HTTP GET
In GET method we cannot send large amount of data rather limited data is sent because, GET method is used to appends form data to the URL in name or value pair. If you use GET, the length of URL will remain limited.
HTTP POST
In POST method large amount of data can be sent because the request parameter is appended into the body. POST is a method that is supported by HTTP and depicts that a web server accepts the data included in the body of the message.
public void GetCaseStatus(string CaseNumber)
{
HttpClient client = new HttpClient();
// Send CaseNumber as Key Value to Post method
var values = new Dictionary<string, string> { { "appReceiptNum", CaseNumber } };
var content = new FormUrlEncodedContent(values);
string url = "https://egov.uscis.gov/casestatus/mycasestatus.do";
// Make a post call using HttpClient to Web with url and content
var response = client.PostAsync(url, content).Result;
var responseMessage = response.Content.ReadAsStringAsync().Result;
// Create a HTML Document to parse and read html tags
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
//SelectNodes method will retrievs all html elements specified (h1)
var caseStatus = htmlDocument.DocumentNode.SelectNodes("h1").FirstOrDefault();
return caseStatus.InnerText;
}
PreviousNext