thmschk
thmschk

Reputation: 644

specify param in R plumber

I created a REST API in R using plumber.

#* description
#* @param id Unique ID  
#* @get /data/<id:int>

where id is required to be numeric. However, plumber also sets required = true. How can I set type of id to numeric (integer) without setting required to true? Cannot find any hints in the manual.

Upvotes: 1

Views: 1252

Answers (2)

Gerda
Gerda

Reputation: 43

Try:

#* description
#* @param id:int Unique ID  
#* @get /data
function(id=NULL) {}

Source:

Is there a way to add optional parameter in API using R Plumber?

https://www.shirin-glander.de/2018/01/plumber/

Upvotes: 0

blairj09
blairj09

Reputation: 11

It would be helpful to see a generic definition of the function for this endpoint. Not wanting id to be required indicates the desire to allow the function to work without a param. To accomplish this, you could define a second endpoint that contains only the root part of the dynamic path:

library(plumber)

#* Simple ID endpoint
#* @param id Unique ID
#* @get /data/<id:int>
function(id) {
  list(
    id = id,
    type = typeof(id)
  )
}

#* Data endpoint
#* @get /data
function() {
  list("Data endpoint")
}

Given this, requests to /data/<id> will return information based on id, while requests to /data can return something else entirely.

Upvotes: 1

Related Questions