-
-
Notifications
You must be signed in to change notification settings - Fork 413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
204 - no content with Maybe #208
Comments
Maybe it's time to consider changing the current combinators... In another issue (or PR?), someone already suggested that we might want to make the status code sent with the response a type-level parameter, so that people can say "I want to return 200 if everything goes fine", etc. Then your suggestion amounts to being able to specify not just one status code but several, which hints at a list of status codes? Regarding your last question, this is how I usually go about it: I write combinators that do what I want in the projects I need them for and if/when I'm satisfied with them I offer to include them in the next release. This means you always have an escape hatch to do things the way you want them done and can easily offer to share your work by creating a PR to include your combinators. In this precise case though, I'm sincerely not sure what's the best way forward for the library to address your needs.
I'd probably go for the second one, but not sure yet. I'd love to hear people's thoughts on this. |
There are cases where the exact successful status code can be determined only in runtime (usually after IO with DB) and this is supported for examle with the decision graph of Erlangs Webmachine. In my case, it would be enough to handle Here, my beginner's haskell knowledge start's to go nowhere... Is this solvable in some easy to grasp way? Bit of googling says that as One more note, would you consider getting all the status codes that particular API endpoint can return (or specifically our handler) to be useful? Bundled with some documentation I would consider this a great help when implementing client for the API. |
Yeah, that overlapping instance is going to cause a lot of trouble. Because Hence my comment in the previous reply: this requires either writing new combinators or modifying the existing ones. I'm not "arguing" about the benefits: with a comprehensive presentation of the status codes and responses that can be sent back to clients, we have a much greater deal of information which is all for the best: the API can be described very precisely by the API docs and client functions can understand much better what's going on when it doesn't get the expected "everything went well" response, because they know what status codes are expected. I know @jkarni is interested in those ideas, however, this represents quite some work and I'm not sure we want to maintain a whole other set of combinators that let us describe APIs more comprehensively. |
We talked about 204 for That's from the perspective of the library. For your specific use case, you can use the |
Actually, how about a new datatype, isomorphic to |
I have used |
@alpmestan Since you're asking for people's thoughts: I think being able to explicitly set the status code would be very useful. For example, I'm currently trying to interact via a protocol that expects 200 for everything that went well (including POST). I could write a new method, say Post200, that behaves like Post but for the status code, but that feels ... not exactly elegant. @jkarni What exactly is the left escape hatch? Do you just return a Left ServantErr with an errHTTPCode of 200? That would not allow me to send some actual data in the response though, would it? |
@kantp You know, we're always happy to receive any kind of feedback! And interestingly the problem you mention is one that's been on the back of my mind lately. I see two solutions, both of which are mentionned in this PR as well as in this thread.
If we go for the second solution, then we would have: type Get cts a = Get' cts a 200 -- the standard Get returns 200 if successful
type Post cts a = Post' cts a 201
-- etc So that if you're not satisfied with the default status code, you can just use the general form and use another status code there. However this solution doesn't support having several "successful status codes", like the 201 or 204 case mentionned in the first comment of this issue. Now, I'll be brutally honest. Those are not the solutions I actually want to go for. I'm starting to think With that said, I don't have any concrete idea for making this happen. Just throwing this out there so that I'm not brainstorming on this alone =) |
Yup. The solution we went with is explicitly declaring the status code, rather than doing |
Does #276 really address this issue? "My endpoint upon POST should return data with 201 created or 204 no content." It seems to me that #276 lets you choose only one (status code, payload data type) per endpoint. I think this issue requires the ability to choose between multiple options like '[(201, some object), (204, no content)]. |
My work around was to throw an custom error with 204 if there was no content i.e. throwError $ ServantErr { errHTTPCode = 204 |
Yeah, as people have mentioned above, the thing to do for e.g. a PUT is return 204 No Content if updating an existing resource, and 201 Created if the resource didn't already exist. Which is something determined at runtime and thus doesn't work to have in the type signature. |
I'm hitting this in 2020 - it would be good to be able to choose the |
@gwils And 2020 has a solution :-) https://github.com/haskell-servant/servant-uverb - not quite the one you describe, but close enough I suppose. |
Thanks - there's a bright future ahead! |
Hello,
thanks for this great lib! It's such a joy to see the types doing great work in web apis 👍
I have one question. My endpoint upon POST should return data with 201 created or 204 no content. I model this using the
Maybe
.Is there any other way to return 204 other than returning
()
as a result? Is extending Servant using new combinators the way to go?The text was updated successfully, but these errors were encountered: