Fork me on GitHub
#reitit
<
2022-12-08
>
vonadz14:12:15

Is there a way to set the expected request content type for a route? I have a route that looks like this:

["notify/" {:post {:name "notify"
                          :tags ["api" "aws"]
                          :summary "receive notifications from AWS"
                          :parameters {:body [:map
                                              [:Type :string]
                                              [:MessageId :string]
                                              [:TopicArn :string]
                                              [:Message :string]
                                              [:Timestamp :string]
                                              [:SignatureVersion :string]
                                              [:Signature :string]
                                              [:SigningCertUrl :string]
                                              [:UnsubscribeUrl :string]]}
                          :responses {200 {:body [:map
                                                  [:message :string]]}}
                          :handler ping}}]
ignore the handler. I'm running into issues where I'm getting a 400 Bad Request error for messages sent from AWS, because they're sending a POST request with a body that looks like JSON, but the content type is set to text/plain instead of application/json.

vonadz15:12:14

Is there some middleware that allows me to coerce the content type header from text/plain to application/json

vonadz15:12:02

found this relevant discussion: https://clojurians.slack.com/archives/C7YF1SBT3/p1572084725199500 Looks like I just need to handle it in the handler

ikitommi15:12:33

there is nothing bulit-in, but you could: • add something custom into the route-data, e.g. :content-type "application/json" • a new middleware that reads the route data (via :compile hook) and based on that, swaps the content-type header. That needs to be mounted before the muuntaja content negotiation middleware to have effect

vonadz16:12:48

@U055NJ5CC not sure what you mean for the first part adding content-type. Yeah second part sounds like too much work. I think it'll be easier to just handle it in the handler