Search code examples
perlmojolicious

With Mojolicious, how do I tell if I am receiving a file directly or via a form?


Using Mojolicious, how can I tell on the server side if a request was made like this:

curl -X POST --data-binary "@/home/me/some/file.jpg" \
  http://127.0.0.1:3000/tasks/some_task

or like this:

curl -X POST \
  -F "userid=1" \
  -F "filecomment=This is an image file" \
  -F "image=@/home/me/some/filejpg" \
  http://127.0.0.1:3000/tasks/some_task

How can I identify differences - I suppose on the Mojo::Request object?


Solution

  • You can't really because the first request makes no sense. It doesn't send a form, but it sends a Content-Type header identifying the content as form data.

    The first request should use something like the following:

    -H 'Content-Type: application/octet-stream'
    

    That would allow the server to distinguish based on content type. A form will have a Content-Type multipart/form-data or application/x-www-form-urlencoded instead.