ngrunwald / ring-middleware-format

Ring middleware for parsing parameters and emitting responses in JSON or other formats
163 stars 49 forks source link

Can't drop charset from media type parameters #63

Open ccann opened 7 years ago

ccann commented 7 years ago

My API has to conform to the JSON API format http://jsonapi.org/format/

I referenced https://github.com/ngrunwald/ring-middleware-format/commit/6f2930789c1bf43c4001bc4fb19fcab284884a99 and built a custom encoder with application/vnd.api+json as content type but then the charset media type parameter is added. I tried specifying :charset "" in wrap-restful-response options and got an exception re unsupported charset.

My solution is this janky middleware:

(defn wrap-mime-type
  "Middleware to replace existing json MIME type with JSON-API MIME type,
   dropping charset."
  [handler]
  (fn [request]
    (let [response (handler request)
          headers {"Content-Type" "application/vnd.api+json"}]
      (if (-> response
              (get-in [:headers "Content-Type"])
              (str/includes? "application/json"))
        (update-in response [:headers] merge headers)
        response))))

Is there a way to do this that I missed?

Thanks!