简体   繁体   中英

How to consume multiple REST API in C#

Initially, I have consumed a single REST API request in C# and desearilsed the JSON response. I want to consume multiple API(2 or 3). How do I modify my code for that?

static void Main(string[] args)
{
    api1();
}

public static void api1()
{
    var client = new RestClient("https://dummy.restapiexample.com/api/");
    var request = new RestRequest("Data");
    var response = client.Execute(request);

    if (response.StatusCode == System.Net.HttpStatusCode.OK)
    {
        string rawResponse = response.Content;
        var root = JsonConvert.DeserializeObject<Rootobject>(rawResponse)
    }
}

I tried to create function for each API request but I am not sure what will be the return type and how am i going to call all the functions.

public async Task<Var> api2()
{
    var client = new RestClient("https://dummy.restapiexample2.com/api2/");
    var request = new RestRequest("Data");
    var response = client.Execute(request);

    if (response.StatusCode == System.Net.HttpStatusCode.OK)
    {
        string rawResponse = response.Content;
        var root = JsonConvert.DeserializeObject<Rootobject>(rawResponse)
        return root;
    }
    else
        return null;
}

static void Main(string[] args)
{
    api1();
    api2();
}

You can't return var as you have done in above, you need to return what are expecting to get from that method, for example, Rootobject .

public async Task Main(string[] args)
{
    var result = await Api2();
    var myProVal = result.MyProperty;
}

public static async Task <Rootobject> Api2()
{
    var client = new RestClient("https://dummy.restapiexample2.com/api2/");
    var request = new RestRequest("Data");
    var response = await client.ExecuteAsync(request);
    if (response.StatusCode == System.Net.HttpStatusCode.OK)
    {
        string rawResponse = response.Content;
        var root = JsonConvert.DeserializeObject<Rootobject>(rawResponse);
        return root;
    }
    else
        return null;
    }
}

And here you have been using async . The async keyword turns a method into an async method, which allows you to use the await keyword in its body. When the await keyword is applied, it suspends the calling method and yields control back to its caller until the awaited task is complete. await can only be used inside an async method. if you don't have such a requirement don't use async, just make the method as synchronous .

Read more: https://learn.microsoft.com/en-us/do.net/csharp/async

https://learn.microsoft.com/en-us/as.net/core/fundamentals/http-requests?view=as.netcore-7.0

Updated:

in the above code snippet, my Rootobject class as follows,

public class Rootobject
{
    public int MyProperty { get; set; }
    public string Name { get; set; }
    public string age { get; set; }
    public string addr { get; set; }
}

In the Main method, MyProperty is a sample property in that class. In your case it should be another one, it's up to you. I think it's better if you can learn the basics before moving to this level. good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM