#ContentConfiguration per request

1 messages · Page 1 of 1 (latest)

somber sable
#

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!

tranquil orchid
#

Yes that will be safe as it's all locked but potentially very slow

somber sable
#

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.

tranquil orchid
#

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

somber sable
#

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

tranquil orchid
#

Yep