lvaudor / glitter

an R package which writes SPARQL queries
https://lvaudor.github.io/glitter
44 stars 5 forks source link

HTTP 400 Bad request on triply endpoint #143

Closed cmdoret closed 1 year ago

cmdoret commented 1 year ago

Glitter looks awesome and I wanted to try it out on the SPARQL endpoint of the Swiss government. As far as I know, it uses triply / YASGUI and I can run the following query via curl:

SELECT * {
  ?s ?p ?o .
} LIMIT 10
$ curl -H 'Accept: text/csv' https://ld.admin.ch/query --data query=SELECT%20%2A%20%7B%0A%20%20%3Fs%20%3Fp%20%3Fo%20.%0A%7D%20LIMIT%2010 -X POST

If that is important, the default response format is XML, which is why I explicitely specified Accept: text/csv.

When trying to run an equivalent query with glitter, I get:

> spq_init() %>%
  spq_add("?s ?p ?o") %>%
  spq_head(n=10) %>%
  spq_perform(endpoint="https://ld.admin.ch/query")
Error in `httr2::req_perform()`:
! HTTP 400 Bad Request.
Run `rlang::last_trace()` to see where the error occurred.
Click for full trace ``` ▆ 1. ├─... %>% spq_perform(endpoint = "https://ld.admin.ch/query") 2. └─glitter::spq_perform(., endpoint = "https://ld.admin.ch/query") 3. └─glitter::send_sparql(...) 4. └─rlang::abort("Not right response type") ```

When trying to spq_perform the query directly, I get:

> spq_perform("SELECT * WHERE {?s ?p ?o } LIMIT 10", endpoint="https://ld.admin.ch/query"
Error in `dplyr::filter()`:
ℹ In argument: `.data$name %in% .query[["prefixes_used"]]`.
Caused by error in `.query[["prefixes_used"]]`:
! subscript out of bounds
Click for full trace ``` --- ▆ 1. ├─glitter::spq_perform("SELECT * WHERE {?s ?p ?o } LIMIT 10", endpoint = "https://ld.admin.ch/query") 2. │ └─glitter::spq_assemble(.query = .query, endpoint = endpoint) 3. │ └─glitter::spq_prefix(.query, auto = TRUE, prefixes = NULL) 4. │ └─... %>% dplyr::filter(.data$type != "Wikidata") 5. ├─dplyr::filter(., .data$type != "Wikidata") 6. ├─dplyr::filter(., .data$name %in% .query[["prefixes_used"]]) 7. ├─dplyr:::filter.data.frame(., .data$name %in% .query[["prefixes_used"]]) 8. │ └─dplyr:::filter_rows(.data, dots, by) 9. │ └─dplyr:::filter_eval(...) 10. │ ├─base::withCallingHandlers(...) 11. │ └─mask$eval_all_filter(dots, env_filter) 12. │ └─dplyr (local) eval() 13. ├─.data$name %in% .query[["prefixes_used"]] 14. └─base::.handleSimpleError(``, "subscript out of bounds", base::quote(.query[["prefixes_used"]])) 15. └─dplyr (local) h(simpleError(msg, call)) 16. └─rlang::abort(message, class = error_class, parent = parent, call = error_call) ```

Am I using glitter wrong, or is there some incompatibility between glitter and some SPARQL endpoints causing this issue?

System information:

maelle commented 1 year ago

@cmdoret for that endpoint you need to use the request_type = "body-form" argument of https://lvaudor.github.io/glitter/reference/spq_perform.html

We added it especially for this endpoint after another user tried it. :grin:

maelle commented 1 year ago
library("glitter") 
spq_init() %>%
  spq_add("?s ?p ?o") %>%
  spq_head(n=10) %>%
  spq_perform(endpoint="https://ld.admin.ch/query", request_type = "body-form")
#> # A tibble: 10 × 3
#>    p                      s                                                o    
#>    <chr>                  <chr>                                            <chr>
#>  1 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85016… http…
#>  2 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85019… http…
#>  3 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85016… http…
#>  4 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85194… http…
#>  5 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85055… http…
#>  6 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85055… http…
#>  7 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85055… http…
#>  8 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85055… http…
#>  9 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85055… http…
#> 10 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85181… http…

Created on 2023-07-19 with reprex v2.0.2

cmdoret commented 1 year ago

Thank you!! I saw this argument but did not connect the dots with the error message :sweat_smile:

maelle commented 1 year ago

Thank you for using glitter, which is a package still in progress. :smile_cat:

maelle commented 1 year ago

@cmdoret for info the syntax changed slightly, see example below

library("glitter") 
spq_init(
  endpoint ="https://ld.admin.ch/query", 
  request_control = spq_control_request(request_type = "body-form")
) %>%
  spq_add("?s ?p ?o") %>%
  spq_head(n = 10) %>%
  spq_perform()
#> # A tibble: 10 × 3
#>    p                      s                                                o    
#>    <chr>                  <chr>                                            <chr>
#>  1 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85016… http…
#>  2 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85019… http…
#>  3 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85016… http…
#>  4 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85194… http…
#>  5 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85055… http…
#>  6 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85055… http…
#>  7 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85055… http…
#>  8 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85055… http…
#>  9 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85055… http…
#> 10 https://gont.ch/canton https://lod.opentransportdata.swiss/didok/85181… http…

Created on 2023-09-21 with reprex v2.0.2