2

I am trying to display and map a response to a class from Unirest with .NET. I have followed this example on stack: Convert the http response body to JSON format using Unirest C#

However, when running it, I get an error "Unable to cast object of type 'System.IO.MemoryStream' to type 'Root Object'.'

My code:

protected void Button1_Click(object sender, EventArgs e)
{
    HttpResponse<JobSearchFeed.RootObject> jsonResponse = Unirest.get("http://api.indeed.com/ads/apisearch?publisher=PUBLISHER_KEY&v=2&l=California&q=javascript")
    .header("X-Mashape-Key", "MY_KEY")
    .header("Accept", "application/json")
    .asJson<JobSearchFeed.RootObject>();
}

I've also looked around and found that I need to use it as a "Task", but I'm not sure if this is even capturing any data at all.

My code for that:

static async Task<JobSearchFeed.RootObject> GetRootInfo()
{
   HttpResponse<JobSearchFeed.RootObject> jsonResponse = await Unirest.get("http://api.indeed.com/ads/apisearch?publisher=PUBLISHER_KEY&v=2&l=California&q=javascript")
    .header("X-Mashape-Key", "MY_KEY")
    .header("Accept", "application/json")
    .asJsonAsync<JobSearchFeed.RootObject>();
    return jsonResponse.Body;
}

My class:

public class JobSearchFeed
{
     public class Result
        {
            public string jobtitle { get; set; }
            public string company { get; set; }
            public string city { get; set; }
            public string state { get; set; }
            public string country { get; set; }
            public string language { get; set; }
            public string formattedLocation { get; set; }
            public string source { get; set; }
            public string date { get; set; }
            public string snippet { get; set; }
            public string url { get; set; }
            public string onmousedown { get; set; }
            public string jobkey { get; set; }
            public bool sponsored { get; set; }
            public bool expired { get; set; }
            public bool indeedApply { get; set; }
            public string formattedLocationFull { get; set; }
            public string formattedRelativeTime { get; set; }
            public string stations { get; set; }
        }

        public class RootObject
        {
            public int version { get; set; }
            public string query { get; set; }
            public string location { get; set; }
            public string paginationPayload { get; set; }
            public int radius { get; set; }
            public bool dupefilter { get; set; }
            public bool highlight { get; set; }
            public int totalResults { get; set; }
            public int start { get; set; }
            public int end { get; set; }
            public int pageNumber { get; set; }
            public List<Result> results { get; set; }
        }
}

1 Answers1

0

The answer is difficult because the project is not easy to follow. Let me follow the way to solution:

  1. The NuGet package is quite old and it targets old NET Framework 4.0 - there are few changes in NET Framework from 4.0. I don't know which target you are using, but I'm sure the newer one. Check the https://www.nuget.org/packages/Unirest-API/ for all specification.
  2. If we check the source project there are 3, and it is difficult to say which one is the correct one: https://github.com/zeeshanejaz/unirest-net, https://github.com/apimatic/unirest-net or https://github.com/Kong/unirest-net. Because the author of the nuget I suspect the first one, but the Project Site points to the githubraw of the second.
  3. When we use pdb to debug original assembly the source code cannot bind in the critical path, which means that something is wrong.
  4. If we copy the source code from the second one to the project it works

I suggest the following:

  1. Copy the source of the library into your project - the library is small and not updated till 2015, so it shouldn't be a big issue
  2. Instead of using not supported library do it yourself with HttpClient and JsonConvert. It will be exactly the same for you, and you skip the problem

The lines with the problem - somehow the code enters this condition and trying to cast object badly:

        else if (typeof (Stream).GetType().IsAssignableFrom(typeof (T).GetType()))
        {
          this.Body = (T) this.Raw;
        }
Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116