user1700890
user1700890

Reputation: 7742

How to send API response without body using Plumber?

Is it possible to send API response without body using Plumber? Here is what I tried:

#* @param msg The message to echo back.
#* @get /echo
function(msg="", res){
  res$body<-NULL
}

and

#* @param msg The message to echo back.
#* @get /echo
function(msg="", res){
 
}

But in both cases in Postman, I end up with {} with either pretty or raw view. It looks like empty body to me, but how can I get rid of {} as well?

Update I was able to achieve this result with python:

import flask
app = flask.Flask(__name__)
app.config["DEBUG"] = True
@app.route('/', methods=['GET'])
def home():
    return "", 200

app.run()

Now, how can I do the same with Plumber?

Another update

I am almost there:

#* @serializer contentType list(type="application/json")
#* @param msg The message to echo back.
#* @get /echo
function(msg="", res){
  
}

Returns no body, success! But I would also like to return status code = 202 (accepted). So I tried:

#* @serializer contentType list(type="application/json")
#* @param msg The message to echo back.
#* @get /echo
function(msg="", res){
  res$status <- 202
}

This almost works, but returns in the body some symbol: �

Now I need to get rid of it as well and keep status code 202

Upvotes: 3

Views: 368

Answers (1)

MrFlick
MrFlick

Reputation: 206566

By default plumber is trying to send a valid JSON response. If that's not what you want, change the serialize to something like text and return an empty string

#* @param msg The message to echo back.
#* @get /echo
#* @serializer text
function(msg="", res){
  res$body <- ""
}

Upvotes: 2

Related Questions