Search code examples
rrestpostrvesthttr

POST a form using R and httr


I try to automaticaly export the data from the csv link of this website https://www.finanssivalvonta.fi/en/capital-markets/issuers-and-investors/Managers-transactions/shortselling/ using R and the package httr an rvest. I tried the following code without success and I don't understand my mistake.

When going on the website and using chrome to see the POST that is done I see the following link https://www.finanssivalvonta.fi/api/shortselling/datatable/current/export. But when using the same link in R I have a status code 500. Do I have to copy all the header/body from the chrome POST? If yes how can I do it?

library(httr)
library(rvest)
res <- POST("https://www.finanssivalvonta.fi/api/shortselling/datatable/current/export")
res$status_code
# 500

I also tried to export directly the table using the following code but the webpage seems not to have finish to load

url <- html_session("https://www.finanssivalvonta.fi/en/capital-markets/issuers-and-investors/Managers-transactions/shortselling/")

url %>% html_nodes("table") %>% .[[1]] %>% html_table(fill=T)

# Error in matrix(NA_character_, nrow = n, ncol = maxp) : 
#   invalid 'ncol' value (too large or NA)
# In addition: Warning messages:
#   1: In max(p) : no non-missing arguments to max; returning -Inf
# 2: In matrix(NA_character_, nrow = n, ncol = maxp) :
#   NAs introduced by coercion to integer range

Many thanks


Solution

  • library(rvest)
    url<-"https://www.finanssivalvonta.fi/en/capital-markets/issuers-and-investors/Managers-transactions/shortselling/"
    
    # Get the session of the URL
    page<-html_session(url)
    
    # RVEST POST the data to the export URL
    page<-rvest:::request_POST(page,url="https://www.finanssivalvonta.fi/api/shortselling/datatable/current/export",
                         encode="form",
                         body=list(
                           "draw"= 2,
                           "columns[0][data]"= "positionHolder",
                           "columns[0][searchable]"= "true",
                           "columns[0][orderable]"="false",
                           "columns[0][search][regex]"="false",
                           "columns[1][data]"="issuerName",
                           "columns[1][searchable]"= "true",
                           "columns[1][orderable]"= "false",
                           "columns[1][search][regex]"="false",
                           "columns[2][data]"="isinCode",
                           "columns[2][searchable]"= "true",
                           "columns[2][orderable]"="false",
                           "columns[2][search][regex]"="false",
                           "columns[3][data]"="netShortPositionInPercent",
                           "columns[3][searchable]"="true",
                           "columns[3][orderable]"="false",
                           "columns[3][search][regex]"= "false",
                           "columns[4][data]"="positionDate",
                           "columns[4][searchable]"="true",
                           "columns[4][orderable]"="false",
                           "columns[4][search][regex]"="false",
                           "start"= 0,
                           "length"= 10,
                           "search[regex]"="false",
                           "lang"= "en",
                           "exportOptions[columnData][positionHolder]"= "Position holder",
                           "exportOptions[columnData][issuerName]" ="Name of the issuer",
                           "exportOptions[columnData][isinCode]" = "ISIN",
                           "exportOptions[columnData][netShortPositionInPercent]"="Net short position (%)",
                           "exportOptions[columnData][positionDate]"="Date",
                           "exportOptions[lang]"="en"
                         ))
    
    writeBin(page$response$content , "data_table.csv")
    

    I used CHROME's advanced tool to track the network traffic when you click "EXPORT in the above URL". I used the same parameters to POST the data and save the result as CSV.