Consume API fipe in C#

Asked

Viewed 1,438 times

2

I’m doing studies in C# and Mysql. I created a combobox calling for Marca and another call Modelo.

I want to run the FIPE api to look for vehicle makes and models. There is this address below stating how to consult, but I would like a more detailed guidance on how to do this in C#.

http://fipeapi.appspot.com/

Obs: these fields (brand, model) will be saved to the bank

Thank you

  • 2

    Welcome to SOPT. What have you tried? Add to question so that the answers have a solution foundation.

  • So far I’ve made no attempt, because I don’t know where to leave...

  • Well if you don’t know where to start the question gets too wide, the api uses JSON can start by giving a read on documentations and examples that manipulate this in the language you want C# in the case, look for example of persistence of mysql data with C# and arise a specific question put here.

1 answer

2

There are several ways to do this, including I don’t know if the question fits as too wide.

Anyway, I’ll show you how to make a request GET using the Restsharp because it greatly facilitates the whole question of making the requests, serializing/deserilizing the data, etc.

It is possible to install it via nuget, with the command

PM> Install-Package Restsharp

Request example GET for http://fipeapi.appspot.com/api/1/carros/marcas.json

public class Program
{
    public static void Main()
    {
        var client = new RestClient
        {
            BaseUrl = new Uri("http://fipeapi.appspot.com/")
        };

        var req = new RestRequest("api/1/{tipo}/marcas.json", Method.GET);

        req.AddParameter("tipo", "carros", ParameterType.UrlSegment);

        var response = client.Execute(req);
        var contentResponse = JsonConvert.DeserializeObject<List<Marca>>(response.Content);
        // ^ Nesta lista vão estar todas as marcas da resposta      
    }
}

public class Marca
{
    public int Id { get; set; }
    public string Key { get; set; }
    public string Name { get; set; }
    public string Fipe_Name { get; set; }
}

Code on Github for future reference

Realize that Marca is a type that should be created by you. response.Content is the return in the original format (JSON) and contentResponse will be a List<Marca>, i.e., the JSON original deserialized.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.