Search code examples
perlmojolicious

Why does Mojolicious have two methods to access POST body?


The Mojolicious has two methods to access POST body: body_params and json

What is the benefit of them?

UPDATE I think, it will be more handy if body_params returns hash if body is recognized by some parser and body_type will return the name of this parser.
Thus if it were POST from form body_type will return application/x-www-form-urlencoded


Solution

  • I cannot tell you why this decision was made. You'd have to ask SRI for that. He added the json method in 2010.

    But I can tell you why it is useful.

    body_params parses requests for common form submissions, application/x-www-form-urlencoded and multipart/form-data. You use that when your action talks to an HTML form submission or similar.

    json on the other hand automatically decodes JSON data from the body. This is useful for AJAXJ requests and APIs. Typically it's used in a REST context where the client sends JSON encoded information. It decodes the JSON directly, making your live easy for you.