Saturday, June 27, 2020

RestSharp :


AddCookie :


request.AddCookie("__ngDebug", "false");
request.AddCookie("Steam_Language", "english");
request.AddCookie("sessionid", "123");

AddHeader :

added to the request you have generated, using request.AddHeader.

request.AddHeader("Cache-Control", "no-cache"); request.AddHeader("Content-Type","application/json");

AddUrlSegment

Replace a token in the request, by using request.AddUrlSegment. This will replace the matching token in the request.

request.AddUrlSegment("token", "1234");
request.AddUrlSegment("username","user");
request.AddUrlSegment("password","pwd");


AddParameter :

a new parameter will add to the request.

req.AddParameter("userLogin", _username,ParameterType.GetOrPost);

 req.AddParameter("password",_password, ParameterType.GetOrPost);


AddBody : we can add body in following ways :

1 ) Anonymous Body :
request.AddBody(new { Id = 1, Name = "Hi", Val = "123" });

2 ) Type Class Body :


public class Registration { public int Id { get; set; } public String Name { get; set; } public string Val { get; set; } }


request.AddBody(new Registration() { Id = 1, Name = "Hi", Val = "100" });


3) AddJsonBody :



 var body = new
            {
                Id = 1,
                Name = "ABC",
                Val = "123",
            };

          
  request.AddJsonBody(body);  or
 request.AddParameter("application/json; charset=utf-8", body , ParameterType.RequestBody);
4) complex object:
var user = new User { apiusername = "aaaaaa", apipassword = "aaaaaa", student_code = "asdhrthsefgserg", username = "asda21", password = "Admin@1", firstname = "qweqwq", lastname = "assaca", email = "as1@asda.com" }; var json = JsonConvert.SerializeObject(user); //Serialize before sending var client = new RestClient( "http://url.net/"); var request = new RestRequest(string.Format("edupro/apis/addstd/{0}", json),Method.GET); IRestResponse response = client.Execute(request); var content = response.Content;

Execute<>(Generic)Method:

// Execute no need to deserialize



var response = client.Execute<Registration>(request); //Registration model class
Assert.That(response.Data.Val, Is.EqualTo("123"), "not correct");



No comments:

Post a Comment