*form *and *json *are examples of "content generators <https://mojolicious.org/perldoc/Mojo/UserAgent/Transactor#GENERATORS>". You can think of them as request macros. *form <https://mojolicious.org/perldoc/Mojo/UserAgent/Transactor#form>*, for example, "generates query string, application/x-www-form-urlencoded or multipart/form-data content" while *json*, for example, "generates application/json content with Mojo::JSON."
So, true, changing the content generator as I recommended changes how the request is sent to the server. But you can also create your own custom generators if you need to, such as an application/x-www-form-urlencoded content expecting a JSON value for a form parameter as yours seems to expect. Indeed, without knowing anything about your API, I recommended a change for how you send your request, completely blindly and contrary to what the API endpoint may be expecting. I assumed the API endpoint would handle a JSON request, but instead it seems to want a JSON string as the value to the *periods* parameter of the form. On Sat, Jun 6, 2020 at 5:50 AM Jeyaraj Durairaj <jeyaraj.durai...@gmail.com> wrote: > Hi Stefan, > > Thanks for the appreciation and the ideas. > > I will surely do the following change in my code. > > changing to return *$tx->result->json*; from return > from_json($tx->result->body); > > However, following change has been tricky to me. > > changing to *json* => { periods => *$c->session('cx_current_quarter')* > }, from form => { periods => encode_json([ > $c->session('cx_current_quarter') > ]) }, > > I, per se, tried *json* => { periods => > *$c->session('cx_current_quarter')* } at the outset. It did not work. > > The reason I found is the following: > > "json =>" works for the API call without "Authorization Header" > my $tx = await $ua->post_p('http://' . $c->config->{API_IP} . > '/api/token/refresh/' => json => { refresh => $refresh_token }); > > only "form =>" works for the API call with Authorization Header" > $tx = await $ua->post_p( > 'http://' . $c->config->{API_IP} . > '/api-app/analytics/summary/', > { Authorization => 'Bearer ' . $access_token }, > *form *=> { periods => *$c->session('cx_current_quarter')* }, > ); > > The difference between json and form in this context was intentional or > accidental. More comments on this will be appreciated! > > Thanks. > -- Jeyaraj > > > > On Saturday, 6 June 2020 02:55:04 UTC+5:30, Stefan Adams wrote: >> >> Great job pushing your application forward with Async/Await! Looking at >> your snippet, here's one small change you can probably make using the json() >> method from Mojo::Message >> <https://mojolicious.org/perldoc/Mojo/Message#json>: >> >> $app->helper(summary_data => async sub ($c, $access_token) { >> my $ua = Mojo::UserAgent->new; >> >> my $tx = await $ua->post_p( >> 'http://' . $c->config->{API_IP} . >> '/api-app/analytics/summary/', >> { Authorization => 'Bearer ' . $access_token }, >> *json* => { periods => *$c->session('cx_current_quarter')* >> }, >> ); >> if ($tx->result->is_success) { >> return *$tx->result->json*; >> } >> $c->flash(message_type => 'warning'); >> $c->flash(message => 'Could not get summary data'); >> return $c->redirect_to('cx-summary', status => 403); >> }); >> >> >> >> On Thu, Jun 4, 2020 at 2:31 AM Jeyaraj Durairaj <jeyaraj...@gmail.com> >> wrote: >> >>> Hi, >>> >>> It works for *helpers* as well with the following declaration. >>> >>> $app->helper(summary_data => async sub ($c, $access_token) { >>> my $ua = Mojo::UserAgent->new; >>> >>> my $tx = await $ua->post_p( >>> 'http://' . $c->config->{API_IP} . >>> '/api-app/analytics/summary/', >>> { Authorization => 'Bearer ' . $access_token }, >>> form => { periods => encode_json([ >>> $c->session('cx_current_quarter') ]) }, >>> ); >>> if ($tx->result->is_success) { >>> return from_json($tx->result->body); >>> } >>> $c->flash(message_type => 'warning'); >>> $c->flash(message => 'Could not get summary data'); >>> return $c->redirect_to('cx-summary', status => 403); >>> }); >>> >>> >>> >>> >>> -- Jeyaraj >>> >>> On Thursday, 4 June 2020 12:06:01 UTC+5:30, Jeyaraj Durairaj wrote: >>>> >>>> Hi, >>>> >>>> Eureka! Eureka! It works!. >>>> >>>> The solution I found (because of my error) is that I uninstalled >>>> Mojo::AsyncAwait and installed Future::AsyncAwait. >>>> It just works! >>>> >>>> By June end, my business intelligence application will be live to serve >>>> 100+ users with insights and analytics. >>>> >>>> (To apply icing on the cake, I have plugged in Moose also in to model >>>> layer for my application) >>>> >>>> As Glen Hinkle said, it is really really a fun to work in Mojolicious. >>>> I love Hypnotoad. >>>> >>>> BTW, I am doing a private project as well to help a home designer to >>>> launch a web site for their small firm. Guess! what framework I am gonna >>>> use.. *Mojolicious.* >>>> >>>> Thanks to Mojo group and special thanks to SRI as well. >>>> >>>> -- Jeyaraj >>>> >>>> >>>> >>>> On Thursday, 28 May 2020 14:15:04 UTC+5:30, Jeyaraj Durairaj wrote: >>>>> >>>>> any luck on the solutions? >>>>> >>>>> -- Jeyaraj >>>>> >>>>> On Thursday, 28 May 2020 00:10:48 UTC+5:30, Jeyaraj Durairaj wrote: >>>>>> >>>>>> However, the below code perfectly work for me outside Mojolicious App. >>>>>> >>>>>> >>>>>> use Modern::Perl; >>>>>> use Mojo::Base -strict, -signatures; >>>>>> use Mojo::UserAgent; >>>>>> use Mojo::Promise; >>>>>> use Mojo::IOLoop; >>>>>> use Mojo::Util 'trim'; >>>>>> use Mojo::AsyncAwait; >>>>>> use LWP::UserAgent; >>>>>> use JSON; >>>>>> >>>>>> >>>>>> my $ua = Mojo::UserAgent->new; >>>>>> my $result = $ua->post('http://localhost:8000/api/token/' => json => >>>>>> { username => 'username', password => 'mypassword' }) >>>>>> ->result >>>>>> ->json; >>>>>> my $access_token = $result->{access}; >>>>>> my $refresh_token = $result->{refresh}; >>>>>> print "Access Token: " . $result->{access}, "\n"; >>>>>> print "Refresh Token: " . $result->{refresh}, "\n\n"; >>>>>> >>>>>> >>>>>> $ua = Mojo::UserAgent->new; >>>>>> $result = $ua->post('http://' . 'localhost:8000' . >>>>>> '/api/token/refresh/' => json => { refresh => $refresh_token }) >>>>>> ->result >>>>>> ->json; >>>>>> >>>>>> >>>>>> my $agent = Mojo::UserAgent->new; >>>>>> async post_user_p => sub ($url) { >>>>>> say "\n\n", $url; >>>>>> my $tx = await $agent->post_p( >>>>>> $url, >>>>>> { >>>>>> Authorization => 'Bearer ' . $result->{access}, >>>>>> }, >>>>>> form => {"username" => "username"}, >>>>>> >>>>>> ); >>>>>> return trim from_json($tx->result->body)->{first_name}; >>>>>> }; >>>>>> >>>>>> async main => sub (@urls) { >>>>>> my @promises = map { post_user_p($_) } @urls; >>>>>> my @names = await Mojo::Promise->all(@promises); >>>>>> say for map { $_->[0] } @names; >>>>>> }; >>>>>> >>>>>> my @urls = (qw( >>>>>> http://localhost:8000/api-converge/auth/user/ >>>>>> )); >>>>>> >>>>>> main(@urls)->wait; >>>>>> >>>>>> >>>>>> >>>>>> >>>>>> On Wednesday, 27 May 2020 23:41:27 UTC+5:30, Jeyaraj Durairaj wrote: >>>>>>> >>>>>>> I tried generating an example app and copied pasted the code segment >>>>>>> you posted here and then tried. >>>>>>> >>>>>>> "Action not found!" error is shown. >>>>>>> >>>>>>> Should I reinstall Perl again and then try? >>>>>>> Will it work under morbo or works only on Hypnotoad? >>>>>>> I am using Windows for my development, wherein morbo is the only >>>>>>> server I can test. >>>>>>> Or can I test it on daemon mode and try> >>>>>>> Please suggest as to how I should diagnose it. >>>>>>> I would need this Async/await to be working in my system. >>>>>>> I have tried without Async, all other functionalities rock both in >>>>>>> development and production (hypnotoad). Mojo::UserAgent is amazing to >>>>>>> fetch >>>>>>> API call data from Django server (running under Apache). >>>>>>> >>>>>>> I am completely counting on Mojolicious as my primary framework for >>>>>>> my new projects. Please help. >>>>>>> >>>>>>> >>>>>>> Regards/Jeyaraj >>>>>>> >>>>>>> On Wednesday, 27 May 2020 17:46:49 UTC+5:30, Sebastian Riedel wrote: >>>>>>>> >>>>>>>> I'll assume it was you who asked the same question on IRC earlier. >>>>>>>> Had you been sticking around for a bit longer you would have seen my >>>>>>>> instructions for getting a working application that i tested locally. >>>>>>>> >>>>>>>> 14:08 <kraih> to be sure i did a "mojo generate app" and then >>>>>>>> replaced the controller with http://paste.scsys.co.uk/589044 >>>>>>>> >>>>>>>> That's the important part, if that doesn't work your Perl or some >>>>>>>> module you installed is broken. >>>>>>>> >>>>>>>> -- >>>>>>>> sebastian >>>>>>>> >>>>>>> -- >>> 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 mojol...@googlegroups.com. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/mojolicious/debdea2d-2f67-4feb-814d-9d936d1b6732%40googlegroups.com >>> <https://groups.google.com/d/msgid/mojolicious/debdea2d-2f67-4feb-814d-9d936d1b6732%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> >> -- > 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+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/mojolicious/ae2e9c20-e50a-4b67-a7a2-b103d7d8b728o%40googlegroups.com > <https://groups.google.com/d/msgid/mojolicious/ae2e9c20-e50a-4b67-a7a2-b103d7d8b728o%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/mojolicious/CACyQ%2BFSgSrwPK4ANizRtixgqBBN7D-8XgKhMvFCc-ENf9UNZZQ%40mail.gmail.com.