I'm trying to build micro service with TMDB API.
Code:
using System.Text.Json;
using Microsoft.Extensions.Options;
using Net5Micro.Config;
using RestSharp;
namespace Net5Micro.Services
{
public class ApiClient : IApiClient
{
private readonly ServiceSettings _settings;
public ApiClient(IOptions<ServiceSettings> settings)
{
_settings = settings.Value;
}
public Movie GetById(string id)
{
//Init rest client
var client = new RestClient($"{_settings.TMDBhost}/movie/{id}");
//Init rest request
var request = new RestRequest(Method.GET);
request.RequestFormat = DataFormat.Json;
//Add Request params
request.AddParameter("api_key", _settings.ApiKey, ParameterType.GetOrPost);
//Calling Api
var response = client.Get(request);
var movies = JsonSerializer.Deserialize<Movie>(response.Content);
return movies;
}
// Za najpopularnije
public Movies Discover(string sort)
{
//Init rest client
var client = new RestClient($"{_settings.TMDBhost}/discover/movie/");
//Init rest request
var request = new RestRequest(Method.GET);
request.RequestFormat = DataFormat.Json;
//Add Request params
request.AddParameter("api_key", _settings.ApiKey, ParameterType.GetOrPost);
request.AddParameter("sort_by", sort, ParameterType.GetOrPost);
//Calling Api
var response = client.Get(request);
var movies = JsonSerializer.Deserialize<Movies>(response.Content);
return movies;
}
public record Movie(int id, string original_title, string poster_path);
public record Movies(int page, Movie[] results);
}
}
GetById()
works fine, but Discover()
throws an error. On debugging I discovered the value returned is text/html
instead of json; I think that's the problem.
And here is the error
Exception has occurred: CLR/System.Text.Json.JsonException
An exception of type 'System.Text.Json.JsonException' occurred in System.Text.Json.dll but was not handled in user code: 'The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.'
Inner exceptions found, see $exception in variables window for more details.
Innermost exception
System.Text.Json.JsonReaderException : The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 0 | BytePositionInLine: 0.at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan
1 bytes) at System.Text.Json.Utf8JsonReader.Read() at System.Text.Json.Serialization.JsonConverter
1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state))
I'm new to .NET so I don't know how to fix it. Thanks in advance.
It returns a HTTP 301 Moved Permanently. Maybe you should follow that move response and access a different URL instead. Get it from the location header as defined in RFC2616.
Your code is missing error handling. It probably should process the response only in case of HTTP 200 OK. Parsing JSON from a HTTP 401 Forbidden does probably not make sense either.