This section in Rocket docs discuss how to implement a data structure for forms, derive that and inject it into controller. However, the problem is it only accepts url-encoded query string in the URL.
It is frequent to get some data from the body of a POST
(and alternatives) request. The documentation discusses this in this section. It offers a FromData
trait, and it has a very small part in the documentation. It redirects us to its API docs, which gives some details as to how to use that.
The example in API docs makes sense, yet FromForm
is far easier to use since it has its derive trait, it has many powerful parts such as Strict, Lenient, field
attribute macro with default values, custom names et cetera.
Currently, you need to implement FromData
trait by hand, there's no derive macro, or attribute macros. So, my question is, given how common it is to process POST
requests, is there an easier way to process POST
processes in Rocket? In my case, the Content-Type
of request will be multipart/form-data
.
Thanks in advance.
Environment
- rustc 1.55.0
- rocket 0.5.0-rc.1 with features
default
andjson
multipart/form-data
, shouldn't you still be usingForm
andFromForm
?FormData
andFromForm
is misleading. It parses two things: either aGET
request with, well, query string likeyoursite.com/foo?baz=baz
or any other type of request (likePOST
,PUT
,DELETE
etc.) with content-type ofapplication/x-www-form-urlencoded
.multipart/form-data
is a different content-type, mainly used to send files (raw bytes) alongside other data. TL;DR It's not possible.