Multipart form requests

So far in this chapter, we have used requests with binary bodies. This is suitable for microservices, but if you want to send files with an HTML form, you should use a request with a multipart/form-data type of content. This allows a client to include multiple files in a single request, but it also needs a parser to split files from the body of a request. The hyper crate doesn't include a parser for multipart requests, and you can use other crates such as the multipart crate to parse requests instead. This, however, doesn't work asynchronously, so you should use the multipart-async crate with the latest versions of the hyper crate. You can also implement multipart requests yourself. To implement this, you can create a struct that implements the Stream trait and parses incoming chunks of data. Multipart requests have the multipart/form-data content type with a boundary value such as boundary=53164434ae464234f. Its body contains a separator and the embedded files:

--------------------------53164434ae464234f
Content-Disposition: form-data; name="first_file"; filename="file1.txt"
Content-Type: text/plain
Contents of the file1.txt
--------------------------53164434ae464234f
Content-Disposition: form-data; name="second_file"; filename="file2.txt"
Content-Type: text/plain
Contents of the file2.txt
--------------------------53164434ae464234f

Your stream has to implement Stream<Item=FileEntry>, which reads a request and extracts files using the provided boundary.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.27.251