Spark: Set response body content is irrelevant

Created on 25 Jan 2017  路  7Comments  路  Source: perwendel/spark

I want to edit the body of the response using
response.body()
But spark routes return String, always.
So if I edit the body, the return override the response body!
Am I missing something?
What is the reason spark routes always return String?

Most helpful comment

After reading this issue, I am still not clear on the semantic of .body()

All 7 comments

Normally you would just return returnString; instead of calling response.body(String returnString);
It's done that way because it made more sense to us compared to editing the body after it's been set.
I think there's a hack where you can do response.body("content");return "";, where the body will not be overwritten by the return value, but I don't have the time to check it out right now.

So could you say that res.body() is not useful?
Is there any reason to use that function?

In fact, @itaied246 , a spark.Route returns an Object. What are you trying to return?

Maybe you could add your custom implementation of spark.ResponseTransformer (i.e.: a json transformer) and use it as the third argument of the overloaded method of the http-verb you are calling. For instance, if its a post, you should use the public static void post(String path, Route route, ResponseTransformer transformer) .

Hope it helps.

So could you say that res.body() is not useful?
Is there any reason to use that function?

In my opinion it's useless/dangerous and shouldn't be used. The only potential use-case I can think of is if you're building/appending the response string in multiple locations, but that's already bad design IMO. I don't know why it's part of the API and I would like to see it gone in version 3.0.

@juliccr I am trying to understand the purpose
res.body() was written for.
Since Route return an object, I don't see a reason to modify the response body.
I was looking for a way to modify the response body, and I find it easier to understand modifying the response body instead of returning an object.

Why closing it? I got to this issue because the actual behavior is misleading. I actually find the body() function useful, I don't see why it is a bad design, and if the method is there spark should consider it if used instead of the return. For now return res.body() at the end of the rout configuration as workaround..

After reading this issue, I am still not clear on the semantic of .body()

Was this page helpful?
0 / 5 - 0 ratings