3

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?

RSDC
  • 43
  • 3
  • BTW, the syntax provided by Mashape is wrong. The code sample you show is an asynchronous call and it's missing the class type. Replace last part with: .asJsonAsync(); - see here for more info (async section) http://unirest.io/net.html - but as a starting point, you can try my synchronous GET code down below to keep things simple. GL. – nanonerd Mar 26 '15 at 15:43

1 Answers1

2

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.

nanonerd
  • 1,964
  • 6
  • 23
  • 49
  • 1
    Shouldn't `MyClass` and `RootObject` be actually the same class? – Arturo Torres Sánchez Mar 18 '15 at 11:57
  • @ArturoTorresSánchez Hi, I think you have the right idea. I changed my answer using your idea + different API and it works. At least for the GET. I still have trouble with POST but I'll create a new Question for that. Thanks. – nanonerd Mar 19 '15 at 07:11
  • 1
    Thanks for the help, will try and let you know how it went. – RSDC Mar 23 '15 at 10:02