Consider the following example of a PUT request, using Plumber (R API):
example_body <- list(
a=1,
b=2,
c=3
)
#* Example PUT endpoint
#* @serializer unboxedJSON list(na = NULL)
#* @param body:object
#* @put /my_example
function(req, body = example_body) {
print(body)
print(req$body$body)
result <- body$a + body$b + body$c
return(result)
}
Testing this example from Swagger works great:
The two print statements show that values of body
and req$body$body
are identical. However, sending a request from anywhere else (not Swagger) means the body is accessed as just req$body
!
A super dirty hack is to put something like the following at the top of the function:
if (grepl(pattern = "/__docs__/|/__swagger__/|/openapi.json",x = req$HTTP_REFERER)) {
req$body <- req$body$body
}
But this is undesirable when you have lots of endpoints. So, to solve this madness, how can the example be used in Swagger while also allowing the endpoint to be used with a real request containing a body?