Search code examples
c#.netmashape

converting mashape api response to c# class


I am using mashape api for getting the speed post tracking information:-

https://www.mashape.com/blaazetech/indian-post

As this is in .NET c# following code is not getting complied:-

Task<HttpResponse<MyClass>> response = Unirest.get("https://indianpost.p.mashape.com/index.php?itemno=EF990403084IN")
.header("X-Mashape-Key", mykey)
.header("Accept", "application/json")
.asJson();

the complie error is "The type arguments for method 'unirest_net.request.HttpRequest.asJson()' cannot be inferred from the usage. Try specifying the type arguments explicitly."

I am not sure how this api can be consumed. Is it problem with "MyClass" and what?


Solution

  • RSDC - Ok, turns out that your API endpoints for Indian-Post don't work anyways. Tested them on Mashape and it returns error.

    >>> I got it working for the metaCritic GET API <<<

    https://www.mashape.com/byroredux/metacritic (Game List API, 2nd one down)

    re: MyClass

    1) On the mashape.com site in the API documentation page, find the 200/JSON response on the right side.

    2) Copy the json data

    3) go to http://json2csharp.com/ and paste the code

    4) click Generate button to get c# class code. Copy the class code.

    5) back in VS, go to Models folder and create class called MyClass.cs.

    6) paste your code in as such:

    public class MyClass
    {
        public class Result
        {
            public string name { get; set; }
            public string score { get; set; }
            public string url { get; set; }
            public string rlsdate { get; set; }
            public string rating { get; set; }
            public string summary { get; set; }
            public string platform { get; set; }
        }
    
        public class RootObject
        {
            public List<Result> results { get; set; }
        }
    }
    

    7) Try this:

            HttpResponse<MyClass.RootObject> response = Unirest.get("https://byroredux-metacritic.p.mashape.com/game-list/ps4/coming-soon")
            .header("X-Mashape-Key", "KxdVFN6Vlymshd5ezOQwBvS2Svjtp1bq5YOjsnFOkgTOwqwM6y")
            .header("Accept", "application/json")
            .asJson<MyClass.RootObject>();
    

    If you run the debugger, you can see that response > Body > results now holds 25 items of data.