POST request with httr not complete (site Fipe table)

2

I'm trying to get the price data from the Fipe table automatically.

Structurally, the information stays in this location:

library(httr)
library(rvest)
url <- "http://veiculos.fipe.org.br/"

read_html(url) %>% 
  html_nodes("td")
{xml_nodeset (16)}
 [1] <td class="noborder"><p>Mês de referência:</p></td>
 [2] <td><p>{mes-de-referencia}</p></td>
 [3] <td class="noborder"><p>Código Fipe:</p></td>
 [4] <td><p>{codigo-fipe}</p></td>
 [5] <td class="noborder"><p>Marca:</p></td>
 [6] <td><p>{marca}</p></td>
 [7] <td class="noborder"><p>Modelo:</p></td>
 [8] <td><p>{modelo}</p></td>
 [9] <td class="noborder"><p>Ano Modelo:</p></td>
[10] <td><p>{ano-modelo}</p></td>
[11] <td class="noborder"><p>Autenticação</p></td>
[12] <td><p>{autenticacao}</p></td>
[13] <td class="noborder"><p>Data da consulta</p></td>
[14] <td><p>{data-consulta}</p></td>
[15] <td class="noborder"><p>Preço Médio</p></td>
[16] <td><p>{preco-medio}</p></td>

The information is empty (as in '{mes-reference}') because a POST request with the desired vehicle data is required.

The data required for POST are indicated in the print below.

ItriedtodoPOSTonthepagebutreturnedStatus405.

POST(url,body=list(codigoTabelaReferencia="215",
    codigoMarca = "2",
    codigoModelo = "4564",
    codigoTipoVeiculo = "1",
    anoModelo = "2015",
    codigoTipoCombustivel = "3",
    tipoVeiculo = "carro",
    modeloCodigoExterno = "",
    tipoConsulta = "tradicional"
  )
)
Response [http://veiculos.fipe.org.br/]
  Date: 2017-07-16 17:16
  Status: 405
  Content-Type: text/html
  Size: 1.29 kB
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>405 - HTTP verb used to access this page is not allowed.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;} 
h1{font-size:2.4em;margin:0;color:#FFF;}
...

Is something missing from POST for it to be valid?

Hugs to all

    
asked by anonymous 16.07.2017 / 19:23

1 answer

2

For each type of query there is a url to make the Request.

In case of the complete query is:

url <- "http://veiculos.fipe.org.br/api/veiculos/ConsultarValorComTodosParametros"

The form for this query is:

httr::POST(
  url,
  encode="form",
  httr::add_headers(Referer = "http://veiculos.fipe.org.br/"),
  body = list(
    codigoTabelaReferencia = "215",
    codigoMarca = "2",
    codigoModelo = "4564",
    codigoTipoVeiculo = "1",
    anoModelo = "2015",
    codigoTipoCombustivel = "3",
    tipoVeiculo = "carro",
    modeloCodigoExterno = "",
    tipoConsulta = "tradicional"
    )
  ) %>% 
  httr::content()
$Valor
[1] "R$ 128.562,00"

$Marca
[1] "Agrale"

$Modelo
[1] "MARRUÁ AM 100 2.8  CS TDI Diesel"

$AnoModelo
[1] 2015

$Combustivel
[1] "Diesel"

$CodigoFipe
[1] "060003-2"

$MesReferencia
[1] "julho de 2017 "

$Autenticacao
[1] "g515kkqlk0cxq"

$TipoVeiculo
[1] 1

$SiglaCombustivel
[1] "D"

$DataConsulta
[1] "terça-feira, 25 de julho de 2017 07:55"
    
25.07.2017 / 12:57