-
Notifications
You must be signed in to change notification settings - Fork 266
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
Returning 406 when mustAccept fails #598
Comments
OK, I worked it out - sorry. Something like this: let handler: HttpHandler =
choose [
mustAccept [ "application/json" ] >=>
choose [
subRoute "/v1" v1Handler
]
RequestErrors.notAcceptable (text "Not accepted")
] |
Actually, wait, no. That means that if the resource is not found I'll get a |
I landed on this helper function, which I'm not overly happy about, but don't know if there's a better way to achieve my goals: let mustAcceptJson (inner): HttpHandler =
let mimeType = "application/json"
choose [
mustAccept [ mimeType ] >=> inner
RequestErrors.notAcceptable (text $"Client does not accept MIME type: {mimeType}")
] I use it like this: route "/user_info" >=> mustAcceptJson (authenticated >=> userInfoHandler) Any suggestions for refinement are welcome. |
OK, after further testing I had to revisit this and I give up trying to use let private jsonMimeType = MediaTypeHeaderValue.Parse("application/json")
let acceptsJson: HttpHandler =
fun (next : HttpFunc) (ctx : HttpContext) ->
ctx.Request.GetTypedHeaders().Accept
|> Seq.exists jsonMimeType.IsSubsetOf
|> function
| true -> next ctx
| false -> RequestErrors.notAcceptable (text $"Client does not accept MIME type: {jsonMimeType}") next ctx This can be plumbed through with the fish operator in an intuitive manner: GET >=> acceptsJson >=> authenticated >=> routef "/something/%O" getSomethingHandler Here's hoping I don't find any more gotchas 🙈 |
Hi @johnnyggalt, thanks for opening this issue and for posting the advances you're making, it could definitely be useful for other people too! I'll try to take a look at this problem during this week. |
So you want:
Is that it? |
I think this is the best approach. For instance, you're doing something similar to what |
Right, but that's why I thought I should be able to use PS. I'm perfectly happy with the implementation of |
Maybe negotiate could be used. |
Also, this PR must be related #502 |
Hi,
Maybe I'm missing something obvious, but I can't find an example of getting
mustAccept
to behave according to best practices when theAccept
header does not match any of the specified content types, which is to return406
. The implementation skips the pipeline in that case, which in my case will result in a404
because that's the reasonable default.How can I use
mustAccept
in a way that allows me to return a406
if it fails?The text was updated successfully, but these errors were encountered: