Comments (8)
OK, after further testing I had to revisit this and I give up trying to use mustAccept
- I just cannot see how to compose it in the manner required. Instead, I wrote my own helper:
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 🙈
from giraffe.
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")
]
from giraffe.
Actually, wait, no. That means that if the resource is not found I'll get a 406
rather than 404
😢 I remain confused and would really appreciate a pointer in the right direction 🙏
from giraffe.
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.
from giraffe.
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.
from giraffe.
Actually, wait, no. That means that if the resource is not found I'll get a
406
rather than404
😢 I remain confused and would really appreciate a pointer in the right direction 🙏
So you want:
- A middleware that checks whether the request has the necessary mime type header (for example,
application/json
), and returns the406
status code if it does not have (short-circuit to the error); - And keep returning the
404
status code if the route does not exist.
Is that it?
from giraffe.
OK, after further testing I had to revisit this and I give up trying to use
mustAccept
- I just cannot see how to compose it in the manner required. Instead, I wrote my own helper: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 ctxThis can be plumbed through with the fish operator in an intuitive manner:
GET >=> acceptsJson >=> authenticated >=> routef "/something/%O" getSomethingHandlerHere's hoping I don't find any more gotchas 🙈
I think this is the best approach. For instance, you're doing something similar to what mustAccept
does: https://github.com/giraffe-fsharp/Giraffe/blob/master/src/Giraffe/Core.fs#L222-L240.
from giraffe.
you're doing something similar to what mustAccept does
Right, but that's why I thought I should be able to use mustAccept
in the first place, but it does not compose in the manner required. I'm curious whether people are using mustAccept
because to me it seems useless because you end up stuck if you want a "standard" experience of 406
for incorrect Accept
headers whilst retaining other things like 404
for undefined routes.
PS. I'm perfectly happy with the implementation of acceptsJson
I came up with, so this is now more a curiosity about mustAccept
than anything else.
from giraffe.
Related Issues (20)
- Migrate to System.Text.Json HOT 1
- Participate on hacktoberfest 2023? HOT 1
- Never decalre reader with `use` on `ctx.Request.Body` HOT 4
- Upgrade to .NET 8 HOT 6
- LinkGenerator doesn't work with routef HOT 1
- Giraffe 6.2 is breaks against Microsoft.IO.RecyclableMemoryStream 3.0.0 HOT 11
- Returning streams, either with WriteStreamAsync or WriteFileStreamAsync or their handler equivalents, is extremely slow HOT 4
- Question: How to approach outside in testing of a micro service? HOT 2
- Guid pattern in endpoint router matches invalid values, throws System.FormatException HOT 6
- Follow-up from "Minor code optimisation #567"
- Update SECURITY.md
- Start using fantomas to validate code submissions with CI HOT 6
- [README] Suggestion for "Getting Started"'s "Doing it manually" HOT 2
- [question] Would it be possible to make `Giraffe.EndpointRouting` case-sensitive? HOT 3
- Remove NuGet API key from the repository HOT 3
- 6.4.0 release references PR for updating to .NET 7 instead of .NET 8 HOT 1
- EndpointRouting - Create endpoint for multiple http verbs
- Request/discussion: WriteStreamAsync buffer size autotuning and/or increase default buffer size HOT 1
- Update `.vscode` debug configuration to point to existent projects
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from giraffe.