Switch between serializers for Plumber API response

Hello,

I have created a simple plumber app

library(plumber)

about <- function(req, res) {
  info <- list(
    version = "0.1.0",
    creator = c("Me", "others"),
    contact = "info@me.com",
    description = "This is a dummy API"
  )
  
  info
}

# Create the Plumber router
pr() %>%
  pr_get(
    path = "/about",
    handler = about
  ) %>%
  pr_run()

However, I would like the user to have the option to specify the format of the content. e.g. http://127.0.0.1:4730/about?format=json or http://127.0.0.1:4730/about?format=rds

I can set one serializer using

pr_get(
    path = "/about",
    handler = about,
    serializer = serializer_rds()
  )

But I want the user to be able to define which one they prefer. I have looked all over but the documentation is very confusing.

Ultimately I would like to create my own serializer but I want to figure this one out first.

Thanks,
PJ

Use two different endpoint?

/about/json and /about/rds?

library(plumber)

about <- function(req, res) {
  info <- list(
    version = "0.1.0",
    creator = c("Me", "others"),
    contact = "info@me.com",
    description = "This is a dummy API"
  )
  
  info
}

# Create the Plumber router
pr() |>
  pr_get(
    path = "/about",
    handler = about
  ) |>
  pr_get(
    path = "/about/json",
    handler = about,
    seriealizer = serializer_json()
  ) |>
  pr_get(
    path = "/about/rds",
    handler = about,
    serializer = serializer_rds()
  ) |>
  pr_run()

The other way would be to dynamically modify the endpoint serializer or manually serialize the response and returning a PlumberResponse. I find it more straightforward to provide different endpoints for different response type.

Hello,

Thanks for your quick response.
I had seen the multiple endpoint one as well, but I would prefer use and argument instead of a whole different endpoint.

Thanks to some of the keywords you provided I was able to come up with this:

library(plumber)
library(jsonlite)

toReturn <- function(req, res, val, format = "json"){
  if(format == "text"){
    res$setHeader("Content-Type", "text")
    res$body = paste(as.character(val), collapse = " ")
  } else if(format == "json") {
    res$setHeader("Content-Type", "application/json")
    res$body <- toJSON(val)
  } else {
    res$status <- 400
    res$body <- sprintf("Error: %s is not a supported format.", format)
  }
  
  res
}

about <- function(req, res) {
  info <- list(
    version = "0.1.0",
    creator = c("Me", "others"),
    contact = "info@me.com",
    description = "This is a dummy API"
  )
  
  toReturn(req, res, info, format)
}

pr() %>%
  pr_get(
    path = "/about",
    handler = about) %>%
  pr_run()

It works but I'm curious if you think this is valid or rather a hack ...

Thanks,
PJ

Everything is a hack :slight_smile: . If it works and you are happy. That's all there is to it, no?

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.