
How to Write GET API Test Using RestSharp?
- Create a test class DemoGetTest and test method Get Operations() in the project.
Operations to be Performed :
- Create request from client and specify the HTTP Method type.
- Send the Request to the Server.
- Get the Response back from the server.
- Validate returned Response’s Body.
Code Snippet :
public class DemoGetTest { private string BaseUrl = "https://reqres.in/"; [Test] public void GetOperationUsingRestSharp() { //Creating Client Connection IRestClient restClient = new RestClient(BaseUrl); //Creating Request from Client to Server IRestRequest restRequest = new RestRequest("/api/users?page=2", Method.GET); restRequest.AddHeader("Accept", "application/json"); //Execute Request on Server IRestResponse response = restClient.Execute(restRequest); var content = response.Content; if (response.IsSuccessful) { Console.WriteLine("Status Code " + response.StatusCode); Console.WriteLine("Response Content " + response.Content); Console.WriteLine("Response Content " + response.Headers); //Console.WriteLine(restResponse.IsSuccessful); //Console.WriteLine(restResponse.StatusCode); } else { Console.WriteLine("Operation Failed"); //Console.WriteLine(restResponse.ErrorMessage); //Console.WriteLine(restResponse.ErrorException); } Assert.AreEqual(200, (int)response.StatusCode); //Verification of response if (!content.Contains("page")) { Assert.Fail("information is not displayed"); } } }
Great content. Thanks for sharing.
ReplyDeleteThanks & most welcome
Delete