Fork me on GitHub
#reitit
<
2019-06-06
>
manutter5113:06:38

Having some trouble getting multipart coercion working right. Here’s my route, based on looking at the docs and examples:

["/drop" {:coercion rcs/coercion
              :name :email-drop
              :parameters {:multipart {:file multipart/temp-file-part}}
              :middleware [middleware/wrap-internal-error
                           middleware/wrap-formats
                           middleware/wrap-base
                           multipart/multipart-middleware
                           ]
              :post {:summary "Mail drop endpoint for  api."
                     :handler #'maildrop/post-attachments}}]

manutter5113:06:08

when I post a multipart form to this endpoint, I get back this:

{:body "[:spec \"(spec-tools.core/spec {:spec (clojure.spec.alpha/keys :req-un [:spec$45582/file]), :type :map, :leaf? false})\"][:problems []][:type :reitit.coercion/request-coercion][:coercion :spec][:value {}][:in [:request :multipart-params]]",
 :status 400,
 :headers {:Content-Length "0", :Server "http-kit", :Date "Thu, 06 Jun 2019 13:35:18 GMT"}}

manutter5113:06:50

I’m not sure how to interpret that. Am I missing a config step somewhere?

manutter5113:06:33

(The multipart ns in my source code is reitit.ring.middleware.multipart)

ikitommi14:06:29

@manutter51 you could debug using the middleware debugger to see if something is consuming the multiparts already, see https://cljdoc.org/d/metosin/reitit/0.3.7/doc/ring/default-middleware#inspecting-middleware-chain

ikitommi14:06:24

error says there were no parts, e.g. body was an empty map.

ikitommi14:06:10

The examples app have the multipart & swagger-ui to test that it works (with the right mw stack).

ikitommi14:06:01

also, the ui shows how to test with curl (correct content-types etc.)

manutter5114:06:06

Ah, that’s helpful, Just understanding what the error means gives me a big hint. Thanks much!

telekid17:06:32

Howdy! Loving my early experiences with Reitit so far. Any recommendations for how I might generate swagger.json as part of a lein task? I’m trying to prototype a little swagger codegen script, and I’d rather not have to boot my app as part of the process.

telekid19:06:02

Answered my own question. For future reference, you can just build a lein task around something like this:

(->> (app {:request-method :get :uri "/api/swagger.json"})
    :body
    (m/decode "application/json"))

manutter5117:06:38

Hmm, no, it looks like nothing’s reading the body, it seems to exit the middleware with the body unread. I did find that there was already a multipart middleware handler in the top-level router stuff, so I got rid of the duplicate. Moving the multipart middleware higher in the list gets me a spec error containing :pred (clojure.core/fn [%] (clojure.core/contains? % :file)), which makes me think something somewhere is expecting a map with a :file key in it?

kingcode17:06:41

I am trying to understand how the following is a path conflict:

[["/bulk/:bulk-id"]
 ["/:version/status"]]
as I would expect “/bulk/2” and “/2/status” to be easy to distinguish. Any suggestion? Thanks!

ikitommi18:06:51

@kingcode "/bulk/status" would match both routes. Disabling the conflict resolution makes it match in the order they are defined.

ikitommi18:06:51

@manutter51 your parameter definitions say that there is one part called :file and it's a file.

manutter5118:06:03

@ikitommi Ok, thanks — I wondered about that, I’m “cargo-culting” that bit from the examples and wasn’t sure what :file meant in that context

ikitommi18:06:22

the examples have swagger-ui, which sends the parts correctly, check example how to send parts?

manutter5118:06:48

Ok, I’ll have another look, maybe I’ll understand it better this time.

👍 4
manutter5118:06:19

I’m trying to map the Mailgun API to reitit so I can upload email attachments (https://documentation.mailgun.com/en/latest/user_manual.html#routes)

manutter5118:06:28

Can’t quite figure out the ??? part for :parameters {:multipart { ??? }}

manutter5118:06:49

Hmm, I bet that needs to be a complete list of all the multipart params I’m expecting

manutter5118:06:32

Ok, I think I’m over the hurdle, conceptually, just gotta get it all spec’ed out and working

manutter5118:06:39

Thanks again

aarkerio20:06:15

Hi! my first day with reitit-ring. Newbie question: can I set routes with the same path but different HTTP methods:

aarkerio20:06:19

["/login" {:get cont-users/login-page}] ["/login" {:post cont-users/post-login}]

ikitommi20:06:47

@aarkerio welcome! I would recommend you read the ring-guide (https://cljdoc.org/d/metosin/reitit/0.3.7/doc/ring). But, here’s how to do it: ["/login" {:get cont-users/login-page, :post cont-users/post-login}].

aarkerio20:06:53

thanks a lot!