Aug 19, 2018, 8:31 AM
Re: [Master_Sergius] How to write simple API for my cgi script?
Now I want to write simple API to use this script in console or other script. For example if I type: <host>/v1/get_words?word=<some_word>, I should get response in JSON format.
What I should use here?
With your first script, most simply, you could handle both GET and POST request methods, and add a new query parameter "json", which if true serializes the response data structure as json instead of html. But from your second script it looks as though you have handled this in your own way.
How to add this to my script properly? I can use $cgi->path_info, or CGI::Application::Dispatch, or some REST framework or something else??? And how?
As you are aware, CGI is outdated and basically deprecated. I recommend using a modern web application framework, one of what I call, "the big three", Dancer2, Mojolicious or Catalyst. There are others available such as CGI::Application::Dispatch that you mentioned, but those I listed are featuresome and there is much more support.
I recommend starting with Dancer2, its simpler to learn than the others. Its focus is specifically on interfacing the typically low level side of things, but there are various core engines that handle common components such as sessions and templates, and plugins that extend its functionality such as for form and database management.
In my opinion, to do it properly, you would need to look into the following (or equivalents), in order to replace your current application:
- Template(::Toolkit) via Dancer2::Template::TemplateToolkit
- DBI via Dancer2::Plugin::Database
- DBIx::Class via Dancer2::Plugin::DBIC
(This post was edited by Zhris on Aug 19, 2018, 8:36 AM)