0

I'm following this example to use httr to send POST request: How can I POST a simple HTML form in R?

How would I go about storing the resulting table of values into a dataframe? I don't quite understand how to find the values in the response object.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Emma
  • 1
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. It's not clear what your API endpoint is returning. If you do `resp<-POST(url,...)` then `content(resp)` should give you whatever contents was returned. How to turn that into a table will depend on what exactly you get back/ – MrFlick Sep 18 '21 at 21:21

1 Answers1

0

Seems like you probably want rvest::html_table:

library(rvest)
content(resp) %>%
   html_table() %>%
   `[[`(1)
## A tibble: 370 x 8
#   CUSIP     `SECURITY TYPE`   RATE   `MATURITY DATE` `CALL DATE`   BUY  SELL `END OF DAY`
#   <chr>     <chr>             <chr>  <chr>           <lgl>       <dbl> <dbl>        <dbl>
# 1 912796EK1 MARKET BASED BILL 0.000% 12/18/2014      NA             0   100.         100.
# 2 912796EL9 MARKET BASED BILL 0.000% 12/26/2014      NA           100.  100.         100.
# 3 912796EM7 MARKET BASED BILL 0.000% 01/02/2015      NA           100.  100.         100.
# 4 912796DF3 MARKET BASED BILL 0.000% 01/08/2015      NA           100   100          100.
# 5 912796EP0 MARKET BASED BILL 0.000% 01/15/2015      NA           100.  100.         100.
# 6 912796EQ8 MARKET BASED BILL 0.000% 01/22/2015      NA           100   100          100.
# 7 912796ER6 MARKET BASED BILL 0.000% 01/29/2015      NA           100   100          100.
# 8 912796DG1 MARKET BASED BILL 0.000% 02/05/2015      NA           100   100          100.
# 9 912796EU9 MARKET BASED BILL 0.000% 02/12/2015      NA           100.  100.         100.
#10 912796EV7 MARKET BASED BILL 0.000% 02/19/2015      NA           100.  100.         100.
## … with 360 more rows

From the linked question for clarity:

library(httr)
url <- "https://www.treasurydirect.gov/GA-FI/FedInvest/selectSecurityPriceDate.htm"
fd <- list(
    submit = "Show Prices",
    priceDate.year  = 2014,
    priceDate.month = 12,
    priceDate.day   = 15
)
resp<-POST(url, body=fd, encode="form")
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57