Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have first API call which gets me ID and once I have got the ID from this api , only then I need to make the second api get call (/api/<Id>/) . It must happen in the series and not parallelly. How can I do this in RestSharp Specflow in Visual Studio 2022


What I have tried:

Get ID from First API call successfully
Posted
Updated 10-Dec-22 13:20pm

1 solution

If you're using RestSharp in Specflow, you can make the first API call and store the ID in a variable. Then, you can use that variable in the second API call. Here's an example of how you might do this:

// First API call to get the ID
var client = new RestClient("https://www.example.com/api/");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);

// Store the ID in a variable
var id = response.Data.id;

// Second API call using the ID
var client = new RestClient("https://www.example.com/api/" + id);
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);


This will make the two API calls in the correct order, with the second API call using the ID obtained from the first API call.
 
Share this answer
 
Comments
DeePatel25 10-Dec-22 20:17pm    
Thanks, Mike,
My second API call endpoint ("https://www.example.com/api/" + id) is in another layer (.cs ) in another NameSpace (x) where I have all my request methods ( Like Get, POST, PUT) ---Now how to get generated ID from the first API call (.cs file in Y namespace) there in our case var id?
CodeGuru84 11-Dec-22 4:45am    
If you need to pass the ID from the first API call to the second API call, but the second API call is in a different class or namespace, you will need to use some mechanism to share the ID between the two classes. One way to do this is to use a static variable or property in the first class to store the ID, and then access that variable or property in the second class. Here is an example of how this might work:

// First class, where the first API call is made
namespace Y
{
public static class FirstApi
{
// Static variable to store the ID
public static string Id;

public static async Task GetId()
{
// Make the first API call
var response = await client.GetAsync("http://api.example.com/get-id");

// Extract the ID from the response and store it in the static variable
Id = response.json()["id"];
}
}
}

// Second class, where the second API call is made
namespace X
{
public static class SecondApi
{
public static async Task GetData()
{
// Use the ID stored in the static variable in the first class to make the second API call
var response = await client.GetAsync($"http://api.example.com/get-data/{Y.FirstApi.Id}");
}
}
}


In this example, the Id variable in the FirstApi class is marked as static, which means it can be accessed from other classes without having to create an instance of the FirstApi class. The Id variable is set in the GetId method, and then it can be accessed from the SecondApi class using the Y.FirstApi.Id syntax.

Of course, this is just one way to do it, and there are many other ways to share data between classes in C#.
DeePatel25 11-Dec-22 8:13am    
Thanks Mike for your prompt reply...I got your point static variable way...
I am trying as a ContextInjection into constructor way...
Would it be possible from you to have sudo code for that way? Your help is deserve millions of thanks..
Pls whenever get chance
CodeGuru84 11-Dec-22 10:01am    
Yes, it is possible to use constructor injection to pass dependencies to your test classes. Here is an example of how you might do this using RestSharp and SpecFlow in Visual Studio 2022:

Add the RestSharp NuGet package to your project. This will provide you with the necessary classes and methods for working with REST APIs in your project.

In your test class, define a constructor that accepts an instance of the RestClient class as a parameter. This will allow you to inject the RestClient instance into your test class. For example:

public MyTestClass(RestClient client)
{
_client = client;
}


In your test scenario, create an instance of the RestClient class and configure it with the base URL of the API you want to call. For example:

var client = new RestClient("https://example.com/api/");


Use the Inject method of the ContextInjection class to inject the RestClient instance into your test class. This will pass the RestClient instance to the constructor of your test class. For example:

ContextInjection.Inject<mytestclass>(client);


In your test class, you can then use the injected RestClient instance to make API calls. For example:

var request = new RestRequest("/api/{id}/", Method.GET);
request.AddUrlSegment("id", id); // Replace "id" with the actual ID you want to use
var response = _client.Execute(request);


This is just one way to use constructor injection with RestSharp and SpecFlow. Depending on your specific requirements and the details of the API you are working with, you may need to adapt this approach to suit your needs.
DeePatel25 11-Dec-22 8:14am    
As in all across the framework we are using ContextInjection Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900