croservices / cro

Development tools for building services and distributed systems in Raku using the Cro libraries.
https://cro.services/
Artistic License 2.0
88 stars 35 forks source link

Add documentation on multipart/form-data #79

Closed jnthn closed 6 years ago

jnthn commented 6 years ago

Provide an example to show how to get at an uploaded file.

cbk commented 6 years ago

Made a simple example program to convert the body-blob (just cut and paste) back into an Str. But how do I actually get to the body-blob data from request-body? use v6.c; my $buff = Blob[uint8].new(70,82,79,77,32,99,114,111,115,101,114,118,105,99,101,115,47,99,114,111,45,104,116,116,112,58,48,46,55,46,54,10,82,85,78,32,109,107,100,105,114,32,47,97,112,112,10,67,79,80,89,32,46,32,47,97,112,112,10,87,79,82,75,68,73,82,32,47,97,112,112,10,82,85,78,32,122,101,102,32,105,110,115,116,97,108,108,32,45,45,100,101,112,115,45,111,110,108,121,32,46,32,38,38,32,112,101,114,108,54,32,45,99,32,45,73,108,105,98,32,115,101,114,118,105,99,101,46,112,54,10,69,78,86,32,84,69,65,95,72,79,83,84,61,34,48,46,48,46,48,46,48,34,32,84,69,65,95,80,79,82,84,61,34,49,48,48,48,48,34,10,69,88,80,79,83,69,32,49,48,48,48,48,10,67,77,68,32,112,101,114,108,54,32,45,73,108,105,98,32,115,101,114,118,105,99,101,46,112,54,10); my $file = $buff.decode('UTF-8'); print $file;

cbk commented 6 years ago

I came up with this working example to generate an array of lines of text from a uploaded text file.... `post -> 'upload' { request-body-blob -> $bits {

Convert bits back in text...

        my $file = $bits.decode('UTF-8');
        #make an array of lines of text from $file.
        my @data = split "\n", $file; 
         ## Now do something with the resulting @data array....

}`

jnthn commented 6 years ago

In c9bd2112ec made request-body examples come first since they're more often the right thing, and also added a code example showing how multipart/form-data (used for file uploads from browsers) can be handled.

jnthn commented 6 years ago

@cbk Note there's a request-body-text that should save the decode step in your example.