Download File in R with POST while sending data
So you should use a right URL and encode = "form"
in the httr::POST()
.
httr
solution based on the @leo answer:
library(httr)POST("https://www.ishares.com/us/product-screener-download.dl", body = list(productView = "ishares", portfolios = "239561-239855"), encode = "form", write_disk("/tmp/ishares-us-etf.xls"))#> Response [https://www.ishares.com/us/product-screener-download.dl]#> Date: 2016-02-08 06:52#> Status: 200#> Content-Type: application/vnd.ms-excel;charset=UTF-8#> Size: 13.6 kB#> <ON DISK> /tmp/ishares-us-etf.xlshead(readLines(file_path), 5)#> [1] "<?xml version=\"1.0\"?>"#> [2] "<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">"#> [3] "<Styles>" #> [4] "<Style ss:ID=\"Default\">"#> [5] "<Alignment Horizontal=\"Left\"/>"
After findling a bit around with Fiddler I found out that I need to send the data with postfields and then everything works fine.
library("curl")handle <- new_handle()handle_setopt(handle, customrequest = "POST")handle_setopt(handle, postfields='productView=ishares&portfolios=239561-239855')curl_download("https://www.ishares.com/us/product-screener-download.dl", "./data/ishares-us-etf.xls", handle=handle)