Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to use + in req_url_query() #432

Open
AGeographer opened this issue Jan 30, 2024 · 5 comments
Open

Option to use + in req_url_query() #432

AGeographer opened this issue Jan 30, 2024 · 5 comments
Labels
feature a feature request or enhancement url 👑

Comments

@AGeographer
Copy link

For the Nominatim API[1] I need to submit my request using plus signs (+) instead of %20 (spaces). I looked at all parameters of req_url_query(), but couldn't find how to tweak this. Is this not yet implemented?

By the way, no rush, I have used the httr package in the meantime...

[1] Sample API call: https://nominatim.openstreetmap.org/search?q=135+pilkington+avenue,+birmingham&format=xml&polygon_kml=1&addressdetails=1

Thanks,
Tim

@hadley
Copy link
Member

hadley commented Jan 30, 2024

Can you show the code you're using for httr?

@AGeographer
Copy link
Author

AGeographer commented Jan 31, 2024

Sure. See below.

I also found this StackOverflow discussion on the matter: https://stackoverflow.com/questions/1634271/url-encoding-the-space-character-or-20

So, maybe there could be a parameter .encoding = in req_url_query() where you could choose url which would encode whitespace to %20 and form which would encode whitespace to +. Following the discussion in the link above, the plus sign could even be the default for the query part.

library(dplyr)
library(stringr)
library(httr)

x <- tibble(place = "135 Pilkington Avenue",
            city = "Birmingham",
            zip = NA,
            state = NA,
            country = "UK")

req <- 
  paste0('https://nominatim.openstreetmap.org/search?q=',
           str_replace_all(
             paste0(x$place, ", ", 
                    x$city, ", ",
                    if_else(is.na(x$zip), "", paste0(x$zip, ", ")), 
                    if_else(is.na(x$state), "", paste0(x$state, ", ")),
                    x$country),
             "\\s", "+"),
         '&format=jsonv2&limit=1')


get_nominatim <- 
  function(request) {
    r <- GET(request)
    if (r$headers$`content-length` > 2) {
          r %>% 
          content() %>% 
          unlist() %>% 
          matrix(nrow = length(.), byrow = T, dimnames = list(names(.))) %>% 
          t() %>% 
          as_tibble()
    } else tibble()
  }

get_nominatim(req)
#> # A tibble: 1 × 17
#>   place_id  licence        osm_type osm_id lat   lon   category type  place_rank
#>   <chr>     <chr>          <chr>    <chr>  <chr> <chr> <chr>    <chr> <chr>     
#> 1 241320786 Data © OpenSt… way      90394… 52.5… -1.8… building resi… 30        
#> # ℹ 8 more variables: importance <chr>, addresstype <chr>, name <chr>,
#> #   display_name <chr>, boundingbox1 <chr>, boundingbox2 <chr>,
#> #   boundingbox3 <chr>, boundingbox4 <chr>

Created on 2024-01-31 with reprex v2.1.0

Session info
sessionInfo()
#> R version 4.3.2 (2023-10-31 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19044)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=English_Canada.utf8  LC_CTYPE=English_Canada.utf8   
#> [3] LC_MONETARY=English_Canada.utf8 LC_NUMERIC=C                   
#> [5] LC_TIME=English_Canada.utf8    
#> 
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] httr_1.4.7    stringr_1.5.1 dplyr_1.1.4  
#> 
#> loaded via a namespace (and not attached):
#>  [1] vctrs_0.6.5       cli_3.6.2         knitr_1.45        rlang_1.1.3      
#>  [5] xfun_0.41         stringi_1.8.3     generics_0.1.3    jsonlite_1.8.8   
#>  [9] glue_1.7.0        htmltools_0.5.7   fansi_1.0.6       rmarkdown_2.25   
#> [13] evaluate_0.23     tibble_3.2.1      fastmap_1.1.1     yaml_2.3.8       
#> [17] lifecycle_1.0.4   compiler_4.3.2    fs_1.6.3          pkgconfig_2.0.3  
#> [21] rstudioapi_0.15.0 digest_0.6.34     R6_2.5.1          reprex_2.1.0     
#> [25] tidyselect_1.2.0  utf8_1.2.4        curl_5.2.0        pillar_1.9.0     
#> [29] magrittr_2.0.3    tools_4.3.2       withr_3.0.0

@hadley
Copy link
Member

hadley commented Jan 31, 2024

Thanks for the link. My interpretation is different, as I think using + is a dated convention and it's better to default to percent encoding, but it's probably worth making this an option.

@jennybc
Copy link
Member

jennybc commented Jan 31, 2024

This is bringing back memories of r-lib/httr#335.

@AGeographer
Copy link
Author

.., but it's probably worth making this an option.

Option would be appreciated! Thanks!

@hadley hadley reopened this Jan 31, 2024
@hadley hadley changed the title How to replace %20 with + in req_url_query Option to use + in req_url_query() Feb 27, 2024
@hadley hadley added feature a feature request or enhancement url 👑 labels Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement url 👑
Projects
None yet
Development

No branches or pull requests

3 participants