1

The {plumber2} R package recently got released and I'm trying to understand how to leverage an API. This basic example below works as long as I'm passing only one string without comma in my query.

#* Simply print some strings passed in the query
#*
#* @get /
#*
#* @query mystrings:string
#* 
function(query) {
  
  print(query$mystrings)

}

This works : <api_endpoint>/?mystrings=hello

These don't work :

  • <api_endpoint>/?mystrings=hello&mystrings=world

  • <api_endpoint>/?mystrings=hello,world

And return the error :

{"type":"https://datatracker.ietf.org/doc/html/rfc9110#section-15.5.1","title":"Bad Request","status":400,"detail":" mystrings must be a scalar value"}

I can read this in the documentation :

Your API may need array-like input from the query string. plumber2 understands two forms of providing array data: Either by providing the same parameter multiple times, e.g. ?arg=1&arg=2&arg=3, or by separating values with a comma, e.g. ?arg=1,2,3.

How can I pass an array of values in my query string so that it can be further interpreted in my API logic ? My case is about asking several ids and then filtering some data with these ids, to then return it to the user.

1 Answer 1

3

Futher down the page you linked to, it talks about specifying the data type.

You can also specify any of the above as an array, by enclosing it in [...], (e.g. [integer] for an array of integers). Arrays can even be nested (e.g. [[integer]] for an array of arrays of integers).

You are currently saying the value must be a string, but you really want an array of strings. So you need to put the value in braces.

#* Simply print some strings passed in the query
#*
#* @get /
#*
#* @query mystrings:[string]
#* 
function(query) {
  
  print(query$mystrings)

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.