Support for RESTful URLs

46 views
Skip to first unread message

vladac...@gmail.com

unread,
May 12, 2014, 5:43:43 AM5/12/14
to swi-p...@googlegroups.com
Hi,

I am trying to create a RESTful Web service API using SWI-Prolog's HTTP server libraries.
However, I am running into some issues related to implementing the URL scheme for my API. For example, I would like to have a list of available resources at the following URL:

http://example.com/resources

This is quite straight forward and entirely feasible. On top of this, I would like to have access to a single resource identified by the unique ID "1":

http://example.com/resource/1

I would like to manipulate this resource using different HTTP methods (GET, PUT). I had a look at the documentation for library(http/http_path), but I can't figure out how to define this type of URL and bind it to a predicate like resource(+Id).

So I guess my question is: is it possible to implement REST-like URL schemes in SWI-Prolog? If so, is there a particularly relevant piece of documentation I should be looking at?

Thanks in advance,
Vlad

Raivo Laanemets

unread,
May 12, 2014, 7:08:20 AM5/12/14
to swi-p...@googlegroups.com
There is a pack that should do what you want: http://swi-prolog.org/pack/list?p=arouter The pack should work with both 7.x and 6.x series but is tested with the latest 7.x series only.

Jan Wielemaker

unread,
May 12, 2014, 7:08:43 AM5/12/14
to vladac...@gmail.com, swi-p...@googlegroups.com
On 05/12/2014 11:43 AM, vladac...@gmail.com wrote:
> Hi,
>
> I am trying to create a RESTful Web service API using SWI-Prolog's
> HTTP server libraries. However, I am running into some issues related
> to implementing the URL scheme for my API. For example, I would like
> to have a list of available resources at the following URL:
>
> http://example.com/resources
>
> This is quite straight forward and entirely feasible. On top of this,
> I would like to have access to a single resource identified by the
> unique ID "1":
>
> http://example.com/resource/1
>
> I would like to manipulate this resource using different HTTP methods
> (GET, PUT). I had a look at the documentation for
> library(http/http_path), but I can't figure out how to define this
> type of URL and bind it to a predicate like resource(+Id).

As a server address, use

:- http_handler('/resource/', handle_resource, [prefix]).

handle_resource(Request) :-
memberchk(path_info(Resource), Request),
...

Here, Resource is an atom that gives you anything after /resource/

To generate link to a specific resource, you maye use

http_link_to_id(handle_resource, path_postfix(Resource), Link)

Cheers --- Jan


> So I guess my question is: is it possible to implement REST-like URL
> schemes in SWI-Prolog? If so, is there a particularly relevant piece
> of documentation I should be looking at?
>
> Thanks in advance, Vlad
>
> -- You received this message because you are subscribed to the Google
> Groups "SWI-Prolog" group. To unsubscribe from this group and stop
> receiving emails from it, send an email to
> swi-prolog+...@googlegroups.com
> <mailto:swi-prolog+...@googlegroups.com>. Visit this group at
> http://groups.google.com/group/swi-prolog. For more options, visit
> https://groups.google.com/d/optout.

Igor Wojnicki

unread,
May 12, 2014, 7:21:10 AM5/12/14
to swi-p...@googlegroups.com
Hello,

I had a similar problem some time ago. A solution I came up with is to
use 'prefix' option of http_handler/3:

:- http_handler(root(.), state_control, [time_limit(10),prefix]).
:- http_handler(root(log), log, [time_limit(10)]).

state_control(Request) :-
...
memberchk(path(Path),Request),
dispatch(Request,Path).

Depending of the actual path of the request dispatch/2 takes apropriate
actions. Mind that

On 05/12/2014 11:43 AM, vladac...@gmail.com wrote:
> --
> You received this message because you are subscribed to the Google
> Groups "SWI-Prolog" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to swi-prolog+...@googlegroups.com
> <mailto:swi-prolog+...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/swi-prolog.
> For more options, visit https://groups.google.com/d/optout.

--
Igor Wojnicki

Igor Wojnicki

unread,
May 12, 2014, 7:23:43 AM5/12/14
to swi-p...@googlegroups.com
ooooppss pressing 'send' too fast...

Mind that handling /log, through log/1 works as well in this example.

vladac...@gmail.com

unread,
May 12, 2014, 11:28:19 AM5/12/14
to swi-p...@googlegroups.com
Thank you all for the quick answers. I started with the [prefix] option, and it seems to work ok. I will also give the "arouter" package a shot, since it looks like it's built specifically for this task.

Cheers,
Vlad
Reply all
Reply to author
Forward
0 new messages