Kemal routing redirecting a subtree

This code snippet shows you how to redirect an entire subtree and remove the first part of the subtree. This might come in useful, for example when you have outside links referencing different languages (e.g. /de /fr /it) and content paths in them, and you want to serve everything from the same endpoints. Crystal Lang…

Weiterlesen

mailerlite API error code 422 for subscribers endpoint

Today, I was implementing the interfacing between PiCockpit.com and the Mailerlite API (which we use aus our newsletter software). https://api.mailerlite.com/api/v2/subscribers I got the following error message: The POST request was returning 422 as status code. Unfortunately there is little documentation about that on Mailerlite. My implementation was correct (this is Crystal Lang by the way):…

Weiterlesen

Dealing with NIL in JSON::Any in Crystal Lang

One of the challenges when developing with Crystal is that it is not as forgiving about data types as, say, Python or Ruby. nil will keep you occupied. The following error message: cast from Nil to String failed, at /usr/share/crystal/src/json/any.cr:220:5:220 can be solved by: # cast to string, THEN check if it is nil if…

Weiterlesen

How to use catch all routes with Kemal

For a Vue.js single page application – at least on a SUB URL of our page – we want a catch all on our server, which will always render the same template / HTML output to the browser. Kemal is internally based on Radix for routing: https://github.com/luislavena/radix Radix has a Catch All / Glob character:…

Weiterlesen

mongo_orm Exception: missing bson key: publish_acl (Exception)

When you extend your field definitions in the mongo_orm document classes, specifically with embedded documents, you might run into the following runtime (!) error: Exception: missing bson key: <name> (Exception) This is not an error in your code – your code is raising, because the database structure is not as it expects it to be!…

Weiterlesen

mongo_orm custom name for collection

mongo_orm will automatically determine the name for your collection by using the Module and Class name, and appending an “s”. In cases where you want to name your collection, you can simply do this: class PapiTest < Mongo::ORM::Document     collection_name “Buster”     field test : String end get “/” do     pt = PapiTest.new     pt.test = “I want…

Weiterlesen

Instructions for adding a user to a mongo database for mongo_orm

You don’t want your web application to access the database using your database admin user. Additionally you want it to access only one specific database – maybe you have additional databases running in your mongo instance. Prerequisites: mongo_orm expects the database configuration as environment variables or as a file config/database.yml in your project. The config/database.yml…

Weiterlesen

kemal-session does not persist data

https://github.com/kemalcr/kemal-session Analyse what is happening with your session cookie. In my case, the session cookie was reset everytime I went to login. I realized, that my idea of cleaning up the old session as a first statement after successful login, before setting up the new session is not a good idea. I suspect, that the…

Weiterlesen