POST request with httr not complete (Fipe table site)

Asked

Viewed 531 times

2

I’m trying to get the price data from the FIPE table automatically.

Structurally, the information stays at 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-de-reference}') because it is necessary to make a POST request with the data of the desired vehicle.

The data required for the POST is indicated in the print below. fipe

I tried to do the POST on the page but returned the Status 405.

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;}
...

Something is missing in the POST for it to be valid?

Hug to all

1 answer

2


For each type of query there is a Request url.

In the case of the complete consultation is:

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

The form for this consultation 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"

Browser other questions tagged

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