Hi all! Does anyone know if there's a way to set a different ContentConfiguration per request? I'm hoping to implement a Middleware which changes some coder settings depending on the value of a header in the request. I can update ContentConfiguration.global in my Middleware, which works in local testing but I strongly suspect that'd be dangerous behaviour causing thread/concurrency related problems 😉 I've had a look through the Vapor source but haven't found what I'm looking for - can anyone help? Thanks!
#ContentConfiguration per request
1 messages · Page 1 of 1 (latest)
Yes that will be safe as it's all locked but potentially very slow
You can provide a custom coder as per https://docs.vapor.codes/basics/content/#one-off
Vapors documentation (web framework for Swift).
Yes, but then I'd have to write that on every controller method for both decode and encode, right?
Anyway I appreciate the info that messing with the global object would be at least safe. I'll have a look into the speed impact.
Correct
You can also use middleware to rewrite the content type and then specify and custom coder for that custom content type
Or do your check and there's a req.content.decode(String.self, as: .myType) helper as well
Ohhh yeah... so I could extend HTTPMediaType with a new type and then have that type vs .json have different coders registered globally?
and then just set the type in the middleware
Yep