Search code examples
rjsonhttpurl

Handle spaces in URL using httr package


I'd like to know how to have GET from the httr package automatically handle parameter values that have a space in them.

When I paste this in my browser:

http://quickstats.nass.usda.gov/api/api_GET/?key=MYAPIKEY&commodity_desc=AG LAND&short_desc=AG LAND, IRRIGATED - ACRES&short_desc=AG LAND - ACRES&year__GE=2015&state_alpha=NC

the spaces are replaced with %20 and the data is obtained from the site. This is the url that the browser returns:

https://quickstats.nass.usda.gov/api/api_GET/?key=MYAPIKEY&commodity_desc=AG%20LAND&short_desc=AG%20LAND,%20IRRIGATED%20-%20ACRES&short_desc=AG%20LAND%20-%20ACRES&year__GE=2015&state_alpha=NC

The parameters such as "AG LAND" in the object my_commodity_desc and "AG LAND, IRRIGATED - ACRES" in my_short_desc1 have spaces in them and I think that's the issue.

This is the code I'm running to get data from http://quickstats.nass.usda.gov:

# url
nass_url <- "http://quickstats.nass.usda.gov"
# commodity description of interest
my_commodity_desc <- "AG LAND"

# short description of interest (i.e., 'data item' on NASS Quick Stats website)
my_short_desc1 <- "AG LAND, IRRIGATED - ACRES"
my_short_desc2 <- "AG LAND - ACRES"

# query start year
my_year <- "2015"

# state of interest
my_state <- "NC"

# final path string
path_nc_irrig_land <- paste0("api/api_GET/?key=", NASS_API_KEY,
                             "&commodity_desc=", my_commodity_desc,
                             "&short_desc=", my_short_desc1,
                             "&short_desc=", my_short_desc2,
                             "&year__GE=", my_year,
                             "&state_alpha=", my_state)
# Run the query
raw_result_nc_irrig_land <- GET(url = nass_url, path = path_nc_irrig_land)

Solution

  • You can use base R's URLencode (from package utils), which does much the same thing that your browser does:

    URLencode(path_nc_irrig_land)
    #> [1] "api/api_GET/?key=myAPIKey&commodity_desc=AG%20LAND&short_desc=AG%20LAND,%20IRRIGATED%20-%20ACRES&short_desc=AG%20LAND%20-%20ACRES&year__GE=2015&state_alpha=NC"