POST request in Restsharp



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

Tags: , ,
Leave a comment

Your email address will not be published. Required fields are marked *

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!

https://bridgejunks.com/ https://crownmakesense.com/ https://brithaniabookjudges.com/ https://hughesroyality.com/ https://rhythmholic.com/ https://bandar89.simnasfikpunhas.com/ https://www.100calshop.co.il/products/thailand/ https://myasociados.com/ https://solyser.com/ http://konfidence.cz/ https://muscadinepdx.com/ https://bandar89.parajesandinos.com.ve/ https://goremekoop.com/ https://oncoswisscenter.com/ https://www.turunclifehotel.com/bandar89/ https://www.houseofproducts.biz/ https://taimoormphotography.com/
BIJI18 BIJI18 BIJI18