- Oct 05, 2022
- admin
- 0
Precondition: Tourist exists with TouristId: 164944
Sample Request body to create UpdateTourist class
{
“tourist_name”: “Karthik”,
“tourist_email”: ” Karthik123@yahoo.com “,
“tourist_location”: “Hyderabad”
}
Copy the above request body to clipboard and click on “Edit”->”Paste special” -> “Paste JSON as Classes” in Visualstudio
“UpdateTourist” class will be created.
using System;
using System.Collections.Generic;
using System.Text;
namespace RestsharpDemo
{
public class UpdateTourist
{
public int id { get; set; }
public string tourist_name { get; set; }
public string tourist_email { get; set; }
public string tourist_location { get; set; }
}
}
Step2: Set the properties of the “UpdateTourist” class and send the object to the request.AddBody();
[Test]
public void UpdateTouristByTouristId()
{
var restClient = new RestClient("http://restapi.adequateshop.com");
var request = new RestRequest("api/Tourist/164944", Method.PUT); //Pass the Tourist ID
request.RequestFormat = DataFormat.Json;
request.AddHeader("Content-Type", "application/json");
UpdateTourist objUpdate = new UpdateTourist();
objUpdate.id = 164944;
objUpdate.tourist_email = "Karthik123@yahoo.com"; //old value is: Karthik99@yahoo.com
objUpdate.tourist_location = "Hyderabad"; //old value is Chennai
objUpdate.tourist_name = "Kartik";
request.AddBody(objUpdate);
var response = restClient.ExecuteTaskAsync(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() == "OK", "Status code is not OK");
}
Step3: Verify that tourist info is updated
http://restapi.adequateshop.com/api/Tourist/164944