Snippets

Brian Medley smallish example

Updated by Brian Medley

File smallish_example.pl Modified

  • Ignore whitespace
  • Hide word diff
     return($c->render(json => {status => "success"}));
 };
 
+put '/api/v1/hello' => sub ($c) {
+    return($c->render(json => {status => "success is a put"}));
+};
+
+get '/api/v1/hello' => sub ($c) {
+    return($c->render(json => {status => "we like get"}));
+};
+
 app->start;
 
 __DATA__
Updated by Brian Medley

File smallish_example.pl Modified

  • Ignore whitespace
  • Hide word diff
 
 get '/' => 'index';
 
+under (sub {
+    my $self = shift;
+
+    unless ($self->req->json) {
+        $self->render(json => {status => "error", data => { message => "No JSON found" }});
+
+        return undef;
+    }
+
+    my $username = $self->req->json->{username};
+    my $api_key = $self->req->json->{api_key};
+
+    unless ($username) {
+        $self->render(json => {status => "error", data => { message => "No username found" }});
+
+        return undef;
+    }
+
+    unless ($api_key) {
+        $self->render(json => {status => "error", data => { message => "No API Key found" }});
+
+        return undef;
+    }
+
+    unless ("fnord" eq $username) {
+        $self->render(json => {status => "error", data => { message => "Credentials mis-match" }});
+
+        return undef;
+    }
+
+    unless ("68b329da9893e34099c7d8ad5cb9c940" eq $api_key) {
+        $self->render(json => {status => "error", data => { message => "Credentials mis-match" }});
+
+        return undef;
+    }
+
+    return 1;
+});
+
 post '/api/v1/hello' => sub ($c) {
     return($c->render(json => {status => "success"}));
 };
Updated by Brian Medley

File smallish_example.pl Modified

  • Ignore whitespace
  • Hide word diff
 
 Try:<br>
 
-    POST /email/v1/email
-    POST /email/v1/status
+    POST /api/v1/hello
Created by Brian Medley

File smallish_example.pl Added

  • Ignore whitespace
  • Hide word diff
+use Mojolicious::Lite;
+
+use v5.20;
+use experimental 'signatures';
+
+get '/' => 'index';
+
+post '/api/v1/hello' => sub ($c) {
+    return($c->render(json => {status => "success"}));
+};
+
+app->start;
+
+__DATA__
+
+@@ index.html.ep
+
+Try:<br>
+
+    POST /email/v1/email
+    POST /email/v1/status
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.