Consume API fipe in C # [closed]

2

I am doing studies in C # and MySql. I created a combobox called Marca and another call Modelo .

I want to run the fipe api to search for makes and models of vehicles. There is this address below informing you how to consult, but I would like a more detailed guidance on how to do this in C #.

link

Note: These fields (make, model) will be saved in the bank

Thank you

    
asked by anonymous 22.02.2016 / 15:27

1 answer

2

There are several ways to do this, including I do not know if the question does not fit as too broad.

Anyway, I'll show you how to make a request GET using RestSharp because it makes the whole issue of requesting, serializing / deserializing the data, etc. very much easier.

You can install it via nuget with the

  

PM > Install-Package RestSharp

Request example GET to 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 in GitHub for future reference

Note that Marca is a type that you must create. response.Content is the return in the original format ( JSON ) and contentResponse will be a List<Marca> , that is, the JSON original deserialized.     

22.02.2016 / 15:52