- Jan 13, 2020
- admin
- 3
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
3 Comments
SEO Reseller Program
Awesome post! Keep up the great work! 🙂
yabanci
I like the efforts you have put in this, thanks for all the great articles. Claudine Grannie Isador
tek parca
Many thanks very valuable. Will certainly share site with my good friends. Gwenette Victoir Alidia