http://qs1969.pair.com?node_id=11141405

programmingzeal has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,

I want to create/update a webpage on my localhost server based upon specific JSON input sent via HTTP POST request to this server on a specific port by a client machine. The server will parse the JSON data and update the local page. I know about REST API and I know how to parse JSON at client side from web services. But I want to build a REST API server using Perl. How can I do this? Please point some tutorial and/or CPAN APIs.

Thanks

Replies are listed 'Best First'.
Re: REST API Server in Perl
by stevieb (Canon) on Feb 16, 2022 at 03:22 UTC

    I use Dancer2 for all my personal stuff, from my automated grow room environment software to a system that fetches data from a Tesla API that I call from a microcontroller, that displays the battery status in my Tesla to remind me to plug the car in if needed when I pull into my garage. JSON in, process in Perl, JSON out.

    Here's a link to the manual.

      Many thanks for your response.

      Actually due to constraint I can't use dancer now but I will adopt this framework in my future work.

      I intend to utilize sockets for achieving my goal. As this is simple client server problem where machines are on LAN. So how to utilize Perl sockets for developing Rest API server?

        What constraint?

        See Dancer2::Plugin::WebSocket or search the Google for tutorials on using Dancer2 with Websockets such as this one.

        Hope this helps!


        The way forward always starts with a minimal test.
Re: REST API Server in Perl
by hippo (Bishop) on Feb 16, 2022 at 09:57 UTC

    My recommendation is for REST::Application which I have used to construct several REST server applications. There is no tutorial as such but the POD is rather good and the Overview section gives a walk through of how to go about it.


    🦛

Re: REST API Server in Perl
by philipbailey (Deacon) on Feb 16, 2022 at 11:42 UTC

    Personally I would prefer to use one of the standard Perl web frameworks (Dancer, Mojolicious or Catalyst). Dancer is the most minimal, but all 3 have a fair number of dependencies. Your requirements may be simple now, but these things have a way of growing, and you may then find that your lightweight choice will suffer growing pains.

    One low-level option, if you really insist on it, is HTTP::Daemon. That's a subclass of IO::Socket::IP, which means you can play with sockets directly if you choose to. If you don't want to use the module directly, its source code might give you some inspiration.

Re: REST API Server in Perl
by ait (Hermit) on Feb 16, 2022 at 18:49 UTC
Re: REST API Server in Perl
by Fletch (Bishop) on Feb 16, 2022 at 22:14 UTC

    Haven't personally used it but Raisin looks interesting. If I had to do something immediately I'd stick to Mojolicious though as I've got slightly more experience using it. Still haven't had call to use it much but it was easy to whip up something to serve as a slack (mattermost) slash command backend.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.