Handling different types of JSON requests

70 views
Skip to first unread message

Myf White

unread,
Sep 24, 2016, 2:56:16 AM9/24/16
to mojolicious
I'm using a javascript library which has two ways of sending POST requests
  1. using javascripts FormData, content-type: multipart/form-data
  2. or as JSON within the body, content-type: application/json
From the Mojolicious side they both work - I can get a parameter 'foo':
  1. with $c->param('foo')
  2. with $c->req->json->{foo}
From the javascript side there is no advantage to either way, they both work as needed. From the Mojolicious side, Is there any advantage to using either way?

The system is currently set up just for use by browser, but there is an idea to develop an API in the future; would that make either of the options more attractive?

Any advice on the "right" way to do this would be very much appreciated.

Kind Regards,
Myf White

Email:
myfw...@gmail.com

Dan Book

unread,
Sep 24, 2016, 3:09:07 AM9/24/16
to mojol...@googlegroups.com
JSON gives you the option to send more straightforward and possibly nested hashes and arrays. JSON body is a relatively common method of making API requests so there's no reason to avoid it really. The only benefit of multipart/form-data is that it's sent natively by HTML forms so doesn't require javascript (or some other language) to construct the request, but that's not really important for an API.

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+unsubscribe@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Myf White

unread,
Sep 24, 2016, 8:17:10 AM9/24/16
to mojolicious
Perfect. Thanks Dan.

Kind Regards,
Myf White

Email:
myfw...@gmail.com

sri

unread,
Sep 24, 2016, 8:23:37 AM9/24/16
to Mojolicious
The only benefit of multipart/form-data is that it's sent natively by HTML forms so doesn't require javascript (or some other language) to construct the request, but that's not really important for an API.

There's also the advantage that we have validation for forms, but not JSON.


--
sebastian 

Joel Berger

unread,
Sep 24, 2016, 9:21:20 AM9/24/16
to Mojolicious
For validation of JSON apis there are external modules like https://metacpan.org/pod/Mojolicious::Plugin::OpenAPI

Daniel Mantovani

unread,
Sep 24, 2016, 6:45:37 PM9/24/16
to Mojolicious
multipart/form-data also supports binary data natively (i.e. uploading files). You could use Base64 to convert the binary data, but it depends whether the overheads (33% on network and corresponding processing load) may be a problem or not for your particular case.

Myf White

unread,
Sep 25, 2016, 3:47:54 AM9/25/16
to mojolicious
Great. Thanks everyone.

Kind Regards,
Myf White

Phone: 0413 757 052
Email:
myfw...@gmail.com

--

Jan Henning Thorsen

unread,
Sep 28, 2016, 8:21:45 AM9/28/16
to Mojolicious
If you think M::P::OpenAPI is too much, then you can use JSON::Validator instead. Check out these links:

Myf White

unread,
Oct 4, 2016, 5:29:23 AM10/4/16
to mojolicious
Thanks Jan,

I only just got around to looking up JSON::Validator... Nice! I think this will suit my needs for this project better than Mojolicious::Plugin::OpenAPI.

Kind Regards,
Myf White

Phone: 0413 757 052
Email:
myfw...@gmail.com

Reply all
Reply to author
Forward
0 new messages