SerializationException: There was an error deserializing the object of type ...Encountered unexpected character '<'.

lm1212 141 Reputation points
2022-05-19T07:59:28.343+00:00

I have just started making an app (following someone) using a movie API from tmdb. There appears to be an issue with the HTTP request or the api's response, as I am getting the error listed below:

SerializationException: There was an error deserializing the object of type MoviePro.Models.TMDB.MovieSearch. Encountered unexpected character '<'.

I have read through some other similar threads expressing the same issue, but have not been able to resolve my problem yet.
I am not very familiar with deserialization and using associated http classes etc.. yet. (Would like to be).
When I debug and place a checkpoint on the error (line 105) I get the 2 properties "readTimeout" and "writeTImeout" of the response variable "responseStream" set to false. I am finding it hard to investigate further form here and see exactly where the problem is.
If someone is able to take a quick look and tell me where to problem is coming from I will be most grateful.
The GitHub Repository link is pasted below. Thank you.
https://github.com/Lm768184/MPro

Developer technologies | ASP.NET | ASP.NET Core
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2022-05-24T10:34:45.25+00:00

    For the second time, the error is due to your code trying to deserialize HTML. What's confusing, if I understand your response above, is you are expecting an HTML response but the code is expecting JSON. Also, the response must be returning a status 200 (Ok) and not a 4XX error message.

    I'm guessing is the client is calling an HTML page and not a REST service endpoint. That's why you're receiving an HTML stream with a 200 (ok) status. Use the debugger to verify the URL is correct. Also, it looks like you might be using one HttpClient for three different services. Can you verify?

        "TmdbSettings": {  
          "BaseUrl": "https://www.themoviedb.org/",  
          "BaseImagePath": "https://image.tmdb.org/t/p",  
          "BaseYouTubePath": "//www.youtube.com/watch?v=",  
          "QueryOptions": {  
            "Language": "en-US",  
            "AppendToResponse": "videos,release_dates,credits",  
            "Page": "1"  
          }  
        }  
    

    If you read the HttpClient reference documentation the recommendation is one HttpClient for each service because the HttpClient by design is scoped to the application not the request. Use the HttpClient factory pattern to fetch the HttpClient(s) the controller needs (constructor injection).

    Make HTTP requests using IHttpClientFactory in ASP.NET Core

    If you have one REST service and therefore one HttpClient then configure the base URL in the startup configuration. Again, I recommend the HttpClient factory pattern.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.