0

I am trying to run a C# console program:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            Task<HttpResponse<MyClass>> response = Unirest.get("https://wordsapiv1.p.mashape.com/words/cat/rhymes")
                    .header("X-Mashape-Key", "xxx")
                    .header("Accept", "application/json")
                    .asJson();

        }
    }

    internal class MyClass
    {
        public string word { get; set; }
    }
}

But this is giving me the following error:

Error CS0411 The type arguments for method 'HttpRequest.asJson()' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Does anyone have any ideas as to what I may be doing wrong?

Rob
  • 26,989
  • 16
  • 82
  • 98
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427

1 Answers1

2

.asJson(); needs to know what type the json should be deserialized into. In this case, you're using MyClass. Change your code to the following:

HttpResponse<MyClass> response = Unirest.get("https://wordsapiv1.p.mashape.com/words/cat/rhymes")
        .header("X-Mashape-Key", "xxx")
        .header("Accept", "application/json")
        .asJson<MyClass>();

Also, you're not calling the async version of asJson, so the result type is HttpResponse<MyClass>, not Task<HttpResponse<MyClass>>.

Please have a read over the examples here

Rob
  • 26,989
  • 16
  • 82
  • 98
  • 2
    Note that if you use `Dictionary`, that's probably the closest to typeless as you can go in this case. – Jacob May 27 '16 at 02:11