I'm learning a little more about Restful in C# .NET but I'm having some problems when I call an API. I had done a similar project before and it worked, but using the same template for this specific API is no longer working.
The idea is to consult an API (https://pokeapi.co) and check if the typed pokemon exists, maybe print its abilities or something along those lines, but when I make my call, the console returns "An error occurred deserializing the response. "
My code is quite simple, I split it into 3 parts:
Pokeresponse.cs
namespace Pokedex
{
public class PokeResponse
{
[JsonProperty("pokemon")]
public string Pokemon { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
}
}
Program.cs
namespace Pokedex
{
class Program
{
static async Task Main(string[] args)
{
try
{
var pokemonClient = RestService.For<IPokeApiService>("https://pokeapi.co/api/v2");
Console.WriteLine("Informe o pokemon");
string informedPokemon = Console.ReadLine().ToString();
Console.WriteLine($"Consultando informacoes do pokemon {informedPokemon}");
var poke =await pokemonClient.GetAddressAsync(informedPokemon);
Console.WriteLine($"\nAbilities:{poke.Id} ");
Console.ReadKey();
}
catch(Exception e)
{
Console.WriteLine("An Error occurred when searching for the pokemon"+ e.Message);
}
}
}
}
IPokeApiService
namespace Pokedex
{
public interface IPokeApiService
{
[Refit.Get("/pokemon/{pokemon}")]
Task<PokeResponse> GetAddressAsync(string pokemon);
}
}
could anyone give any tips on how to proceed? I tried to debug it but didn't get much progress.
If you take a look at the JSON response from docs you can see that there is no Pokemon field(so rename it to Name), also Id should be int(that was the real cause of exception)
Result:
public class PokeResponse
{
public string Name { get; set; }
public int Id { get; set; }
}