How to send json response using plumber R

前端 未结 2 1713
执念已碎
执念已碎 2020-12-21 00:08

I need to send response from R using plumber package in below format

{
  \"status\": \"SUCCESS\",
  \"code\": \"200\",
  \"output\": {
    \"studentid\": \"1         


        
2条回答
  •  有刺的猬
    2020-12-21 00:47

    I added an unboxedJSON serializer to the development version of plumber. Depending on when in the future this is being read, that serializer might have been published to CRAN and might even be the default serializer now (I'm still debating).

    But for now, you can install the development version from GitHub (devtools::install_github("trestletech/plumber")) then add the @serializer unboxedJSON annotation to your function like so:

    #* @post /sum
    #* @serializer unboxedJSON
    addTwo <- function(){
      list(status = "SUCCESS", code = "200",output = list(studentid = "1001", name = "Kevin"))
    
    }
    

    FYI if you ever do want to force plumber to return some text that you're providing directly, you should be able to set the $body element on res then return the res object from the function.

    #* @get /
    function(res){
      res$body <- "I am raw"
      res
    }
    

    which will return the unformatted, unserialized text I am raw in its response.

提交回复
热议问题