jpquast
jpquast

Reputation: 375

Fetching JSON with GET from GraphQL-based API not possible?

I am trying to query the RCSB GraphQL API in order to get information about protein structures. I am doing this as part of an automated script. Previously I used their Search API, which they however retire in December. Usually using httr always worked for me but I feel like in this case I am missing something.

The first example they suggest on their website (https://data.rcsb.org/#rest-api) is the following:

https://data.rcsb.org/graphql?query={entry(entry_id:"4HHB"){exptl{method}}}

In the browser I can get the wanted information with this easily (https://data.rcsb.org/graphql?query=%7Bentry(entry_id:%224HHB%22)%7Bexptl%7Bmethod%7D%7D%7D), however when I try to do this from R I get a client error 400 Bad Request.

library(httr)
query <- 'query={entry(entry_id:"4HHB"){exptl{method}}}'
r <- GET(modify_url("https://data.rcsb.org/graphql", query = query))
http_status(r)
$category
[1] "Client error"

$reason
[1] "Bad Request"

$message
[1] "Client error: (400) Bad Request"

I also tried many different things but I cannot get this to work somehow. I am relatively new to this so maybe someone can spot a clear mistake. Would be great to get some help!

Upvotes: 1

Views: 212

Answers (1)

Paul
Paul

Reputation: 9107

The query needs to be URL encoded

library(httr)
query <- URLencode('query={entry(entry_id:"4HHB"){exptl{method}}}')
r <- GET(modify_url("https://data.rcsb.org/graphql", query = query))
http_status(r)
#> $category
#> [1] "Success"
#> 
#> $reason
#> [1] "OK"
#> 
#> $message
#> [1] "Success: (200) OK"

Upvotes: 1

Related Questions