2

As I'm getting more familiar with the RestSharp client library for my API testing I'm confortable with using it for single API response checks e.g. checking a status code is correct, or that the response body is as expected.

However, I'm now looking at more complex tests, namely passing parameters from one API to be used in another. Is anyone aware of how this can be done using RestSharp and, if so, an example would be very useful.

I've provided an example below of something similar I've done in RestAssured(Java) but I'm now looking to do this using RestSharp and C#

@Test
public void test_ScenarioRetrieveFirstCircuitFor2017SeasonAndGetCountry_ShouldBeAustralia() {

    // First, retrieve the circuit ID for the first circuit of the 2017 season
    String circuitId = given().
    when().
        get("http://ergast.com/api/f1/2017/circuits.json").
    then().
        extract().
        path("MRData.CircuitTable.Circuits.circuitId[0]");

    // Then, retrieve the information known for that circuit and verify it is located in Australia
    given().
        pathParam("circuitId",circuitId).
    when().
        get("http://ergast.com/api/f1/circuits/{circuitId}.json").
    then().
        assertThat().
        body("MRData.CircuitTable.Circuits.Location[0].country",equalTo("Australia"));
}
2
  • You mean using the response from API 1 to use as request in API 2?
    – FDM
    Sep 21, 2018 at 9:59
  • Yes, that's what I meant sorry. I've provided a code example which should give a bit more clarity too Sep 21, 2018 at 10:10

2 Answers 2

0

All you need to do is store part of the response in a variable (i.e. the data you want to use in the next request) and inject it again.

  1. Execute the request and catch the IRestResponse
  2. Extract the value using JsonPath from the Content property of the response object
  3. If the next request is a POST, load JSON as string and replace a mock value with the value extracted in the previous step. If the next request is a GET, replace a mock value in the URL of the API with the extracted value.
  4. Send your second request with the replaced data.

Example (abbreviated to key actions only)

var client = new RestClient("base url here");
var response = client.Execute(request);

// get a value
var id = JToken.Parse(response.Content).SelectToken("data.id").ToObject<int>();

// for a query parameter in your next call, you might do:
var request = new RestRequest();
request.AddQueryParameter("id", id); // re-use value

// for a POST Json body request, you might do:
var body = @"
{
    "search": {
        "id": {id},
        "name": "test"
    }
}";
body = body.Replace("{id}", id);

// add the body to the request
request.AddParameter("application/json", body, ParameterType.RequestBody);
6
  • that's great thank you. I just need to work out in my head how to code this now Sep 21, 2018 at 10:17
  • Yeah, would show some code but we are using a framework built around RestSharp so it would be more confusing than otherwise. :)
    – FDM
    Sep 21, 2018 at 10:20
  • no problem. It's just difficult seeing some real-world examples for this as that's how I best absorb this sort of thing Sep 21, 2018 at 10:22
  • If you need some code, let me know and I'll try to provide some.
    – FDM
    Sep 21, 2018 at 12:02
  • that would be very handy if you wouldn't mind? Sep 21, 2018 at 12:04
0
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

        var client = new RestClient("url_api_postman_here");
        client.Timeout = -1;
        var request = new RestRequest(Method.POST);
        request.AddQueryParameter("UsuarioDF", UsuarioDF);
        request.AddQueryParameter("ClaveDF", ClaveDF);
        request.AddHeader("key", "Content-Type");
        request.AddHeader("value", "application/json");
        request.AddHeader("type", "text");
        request.AddHeader("Content-Type", "application/json");
        var body = @"{" + "\n" + @"""Username"":""{UsuarioDF}""," + "\n" + @"""Password"":""{ClaveDF}""" + "\n" + @"}" + "\n" + @"";

        body = body.Replace("{UsuarioDF}", UsuarioDF.Trim());
        body = body.Replace("{ClaveDF}", ClaveDF.Trim());

        request.AddParameter("application/json", body, ParameterType.RequestBody);
        IRestResponse response = client.Execute(request);
        Token = response.Content.Trim();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.