- Oct 05, 2022
- admin
- 0
For Restsharp training / support please contact us @ +91 888 588 5855
Step1: Create a class using the request body
Sample Request body to create NewTourist class
{
“tourist_name”: “Karthik”,
“tourist_email”: ” Karthik99@yahoo.com “,
“tourist_location”: “Chennai”
}
Copy the above request body to clipboard and click on “Edit”->”Paste special” -> “Paste JSON as Classes” in Visualstudio
“NewTourist” class will be created.
using System;
using System.Collections.Generic;
using System.Text;
namespace RestsharpDemo
{
public class NewTourist
{
public string tourist_name { get; set; }
public string tourist_email { get; set; }
public string tourist_location { get; set; }
}
}
Step2: Set the properties of the “NewTourist” class and send the object to the request.AddBody();
[Test]
public void CreateNewTourist()
{
var restClient = new RestClient(“http://restapi.adequateshop.com”);
var request = new RestRequest(“api/Tourist”, Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddHeader(“Content-Type”, “application/json”);
NewTourist objNewTourist= new NewTourist(); //create a instance
objNewTourist.tourist_email = “Karthik99@yahoo.com”;
objNewTourist.tourist_location = “Chennai”;
objNewTourist.tourist_name = “Karthik”;
request.AddBody(objNewTourist); //Pass the object of the request class created above
var response = restClient.ExecutePostTaskAsync(request);
while (response.Status != TaskStatus.RanToCompletion)
{
Console.WriteLine(“ThreadID:{0},Status: {1}”, Thread.CurrentThread.ManagedThreadId, response.Status);
Task.Delay(100).Wait();
}
Thread.Sleep(5000);
Console.WriteLine(“Status code is:” + response.Result.StatusCode.ToString());
Assert.IsTrue(response.Result.StatusCode.ToString() == “Created”, “Status code is not Created”);
}
Step3: Verify that new tourist is created
http://restapi.adequateshop.com/api/Tourist?page=1
For Restsharp training / support please contact us @ +91 888 588 5855