Passing Headers, Parameters in RestSharp



For Restsharp training / support please contact us @ +91 888 588 5855

Passing Headers in RestSharp

Use “AddHeader” built-in method to pass the info through header’s.

Passing parameters in RestSharp

We have “AddParameter” build-in method to pass the parameter to Rest API.

Example:

request.AddHeader(“Content-Type”, “application/json”);

 

Sample Code:

Example1:

restClient = new RestClient(“http://localhost/drupal/gpa/node”);

RestRequest request = new RestRequest();

request.Method = Method.GET;

request.AddHeader("Content-Type", "application/json"); //Header name:Content-Type, Header value: application/json

request.AddHeader("X-CSRF-Token", csrftoken);

request.AddHeader("Cookie", sessid);

request.AddParameter("type", "product_activation", ParameterType.GetOrPost);

var response = restClient.Execute(request);




Example2:

[Test]
public void VerifyStatusCode()
{
var restClient = new RestClient("http://restapi.adequateshop.com");//domain name

var request = new RestRequest("/api/Tourist?page= 1", Method.GET);

request.AddHeader("Content-Type", "applicatiom/json");

var response = restClient.ExecuteGetTaskAsync(request);
while (response.Status != TaskStatus.RanToCompletion)
{
Console.WriteLine("ThreadID:{0},Status: {1}", Thread.CurrentThread.ManagedThreadId, response.Status);
Task.Delay(100).Wait();
}

Console.WriteLine("Status code is:" + response.Result.StatusCode.ToString());

Assert.IsTrue(response.Result.StatusCode.ToString() == "OK", "Status code is not OK");

}

For Restsharp training / support please contact us @ +91 888 588 5855

Tags:
3 Comments

Awesome post! Keep up the great work! 🙂

Reply

I like the efforts you have put in this, thanks for all the great articles. Claudine Grannie Isador

Reply

Many thanks very valuable. Will certainly share site with my good friends. Gwenette Victoir Alidia

Reply
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