How does one get variables in paths to work with http_handler/3?

#1

I’m having zero luck expanding the example at http://www.swi-prolog.org/pldoc/doc_for?object=http_handler/3#http:location/3

If I try something like:

:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_unix_daemon)).
:- use_module(library(http/http_files)).
:- use_module(library(http/http_error)).
:- use_module(library(http/html_write)).

:- initialization http_daemon.

:- multifile http:location/3.

http:location(files, '/user', []).

:- http_handler(files(user/User), user(Method, User),
                [ method(Method),
                  methods([get,post,put])
                ]).

user(get, User, _Request) :-
  reply_html_page(
    [title('Hello World!')],
    [p(User)]
  ).

I just get an error Arguments are not sufficiently instantiated.

What is the magic incantation to make this work?

#2

Except for the :- initialization http_daemon., it works fine. This declaration leads to a double startup. The http_unix_daemon library uses initialization/2.

Test:

swipl s.pl --interactive --port=4001
% Started server at http://localhost:4001/
101 ?- 
curl http://localhost:4001/user/user/jan
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

</head>
<body>

<p>
jan</p>
</body>
</html>
#3

Thanks Jan

I’m using version 7.4.2, so maybe I need to upgrade. I’ll install the latest version and see if I can replicate what you did.

#4

Yip. This has been added rather recently. I think you need 8.1.x (currently 8.1.5).

#5

Thanks Jan

Problem solved by upgrading to 8.1.5.

Following the build instructions worked flawlessly on my home PC, but was a bit more challenging on my web hosting company’s server where CentOS 7 had a too old version of cmake, so I had to install a newer version in my home directory, and then make failed because of tex, so I had to edit the CMake file to turn documentation off… but all good now.

#6

The build doesn’t use LaTeX. It does various packages that are not so obvious such as archive. That should eventually be avoided by restructuring the modules that constitute PlDoc. It is wise to install all prerequisites to avoid these issues. You find the list for Redhat/Fedora/CentOS here

1 Like