1

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.

urmat abdykerimov
  • 427
  • 1
  • 7
  • 17
  • did the answer below help you? – urmat abdykerimov Jul 21 '21 at 08:02
  • Sorry, i thought i answered you yesterday, but now i cant see my comment. I guess i didn't submit it. Anyway, your answer helped me, thanks. –  Jul 22 '21 at 14:30
  • Well, if you know how the system works, than you can understand this response that is given to me when i give the feedback: "Thanks for the feedback! You need at least 125 reputation to cast a vote, but your feedback has been recorded." –  Jul 24 '21 at 04:57

1 Answers1

0

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; }
}
urmat abdykerimov
  • 427
  • 1
  • 7
  • 17