3.2 library(http/http_dispatch): Dispatch requests in the HTTP server
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • SWI-Prolog HTTP support
        • The HTTP server libraries
          • library(http/http_dispatch): Dispatch requests in the HTTP server
            • http_handler/3
            • http_delete_handler/1
            • http_dispatch/1
            • http_request_expansion/2
            • http_current_handler/2
            • http_current_handler/3
            • http_location_by_id/2
            • http_link_to_id/3
            • http_reload_with_parameters/3
            • http_reply_file/3
            • http_safe_file/2
            • http_redirect/3
            • http_404/2
            • http_switch_protocol/2
Availability::- use_module(library(http/http_dispatch)).
Sourcehttp_link_to_id(+HandleID, +Parameters, -HREF)
HREF is a link on the local server to a handler with given ID, passing the given Parameters. This predicate is typically used to formulate a HREF that resolves to a handler implementing a particular predicate. The code below provides a typical example. The predicate user_details/1 returns a page with details about a user from a given id. This predicate is registered as a handler. The DCG user_link//1 renders a link to a user, displaying the name and calling user_details/1 when clicked. Note that the location (root(user_details)) is irrelevant in this equation and HTTP locations can thus be moved freely without breaking this code fragment.
:- http_handler(root(user_details), user_details, []).

user_details(Request) :-
    http_parameters(Request,
                    [ user_id(ID)
                    ]),
    ...

user_link(ID) -->
    { user_name(ID, Name),
      http_link_to_id(user_details, [id(ID)], HREF)
    },
    html(a([class(user), href(HREF)], Name)).
HandleID is either an atom, possibly module qualified predicate or a compound term if the hander is defined using a pattern. See http_handler/3 and http_location_by_id/2.
Parameters is one of

  • path_postfix(File) to pass a single value as the last segment of the HTTP location (path). This way of passing a parameter is commonly used in REST APIs.

    New code should use a path pattern in the handler declaration and a term‘HandleID(Arg, ...)`

  • A list of search parameters for a GET request.

See also
http_location_by_id/2 and http_handler/3 for defining and specifying handler IDs.