restbed icon indicating copy to clipboard operation
restbed copied to clipboard

Getting raw picture bytes from POST multipart/form-data request

Open vbs96 opened this issue 8 years ago • 2 comments

Hi,

I'm wandering if there is a way to get the bytes of an image that I upload using POST (curl command: curl -F "[email protected]" http://localhost:8080/v2/stream/frames/ I searched through examples and I couldn't find a proper way to do so. I tried using the following code:

void FrameApiStreamFramesResource::POST_method_handler(const std::shared_ptr<restbed::Session> session) {
   
   auto request = session->get_request();
    int content_length = request->get_header("Content-Length", 0);
    session->fetch((size_t)content_length, [request](const std::shared_ptr<restbed::Session> session, const restbed::Bytes& body)
                   {
                       fprintf( stdout, "Body size: %d\n", (int)body.size());
                       fprintf( stdout, "%.*s\n", static_cast< int >( body.size( ) ), body.data( ));
                       session->close( restbed::OK, { { "Connection", "close" } } );
                   });
}

But the body.data() contains, along with the data I need, some headers and other info. Is there any way to get exactly the raw bytes of the picture I uploaded or I need to manually parse the request?

Thanks.

vbs96 avatar Sep 06 '18 18:09 vbs96

Interesting, the body should not have header information inside unless you are transmitting such data.

Will investigation this for the 4.7 release.

ben-crowhurst avatar Feb 08 '20 02:02 ben-crowhurst

Sorry, I read your issue with too much haste. You are seeing headers in the body due to the curl option -F.

   -F, --form <name=content>
          (HTTP)  This  lets curl emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data using the Content-Type
          multipart/form-data according to RFC 2388. This enables uploading of binary files etc. To force the 'content' part to be a  file,  prefix  the  file
          name with an @ sign. To just get the content part from a file, prefix the file name with the symbol <. The difference between @ and < is then that @
          makes a file get attached in the post as a file upload, while the < makes a text field and just get the contents for that text field from a file.

          Example: to send an image to a server, where 'profile' is the name of the form-field to which portrait.jpg will be the input:

           curl -F [email protected] https://example.com/upload.cgi

          To read content from stdin instead of a file, use - as the filename. This goes for both @ and < constructs. Unfortunately it does not support  read-
          ing the file from a named pipe or similar, as it needs the full size before the transfer starts.

          You can also tell curl what Content-Type to use by using 'type=', in a manner similar to:

           curl -F "[email protected];type=text/html" example.com

          or

           curl -F "name=daniel;type=text/foo" example.com

          You can also explicitly change the name field of a file upload part by setting filename=, like this:

           curl -F "file=@localfile;filename=nameinpost" example.com

          If filename/path contains ',' or ';', it must be quoted by double-quotes like:

           curl -F "file=@\"localfile\";filename=\"nameinpost\"" example.com

          or

           curl -F 'file=@"localfile";filename="nameinpost"' example.com

          Note that if a filename/path is quoted by double-quotes, any double-quote or backslash within the filename must be escaped by backslash.

          See further examples and details in the MANUAL.

          This option can be used multiple times.

          This option overrides -d, --data and -I, --head and --upload.

As per man curl documentation this produces a 'multipart/form-data according to RFC 2388'

ben-crowhurst avatar Feb 11 '20 00:02 ben-crowhurst