Is it possible to create real REST APIs in XP?

Enonic version: 6.8.0
OS: Windows 10

Hi,

I just got an idea for a new application (a text snippet store, with macros, etc.), and wanted to build this around a REST API. Basically, I need to be able to do CRUD operations. (I am planning this for XP 6.9.0+, when the Node API is released, so I will not use the normal content API) So I started building a service, but I think I’ve hit a problem:

The URLs to the services are defined as */_/service/[application]/[service-name], while I would like to do have */_/service/[application]/[service-name]/[text-snippet-id] for read, update and delete operations.

Is this currently (or in the future) possible to implement in XP?

I realize I can just send the ID as params or as in the request body, but I was hoping to build a truly RESTful web service. My hope is that it could be possible to define routes in a service for everything that maps under that path.

Any thoughts?

I think you could do this with Controller Mappings:
http://xp.readthedocs.io/en/latest/developer/site/mappings/index.html

A controller mapping is basically the same as a service, but where you can specify the URL pattern (relative to the Site). The controller can handle GET, POST, DELETE, etc, just like any other app controller.

The URL would not be based on */_service/xxx but on your site root path, and you can add a virtual path to handle the REST API. In the example below, it does not need to exist a content with path /rest/product.

<site>

  <mappings>
    <mapping controller="/site/rest-lib/product-get-create.js" order="10">
      <!-- URL relative to site path, e.g.:  [site-path]/rest/product -->
      <pattern>/rest/product</pattern>
    </mapping>
    <mapping controller="/site/rest-lib/product-list.js" order="10">
      <pattern>/rest/product/list</pattern>
    </mapping>
    <mapping controller="/site/rest-lib/product-delete.js" order="10">
      <pattern>/rest/product/delete/.*</pattern>
    </mapping>
  </mappings>

</site>
4 Likes

Nice! That is exactly what I was looking for. Thanks!

Goody, but neighter are real REST api’s I guess :slight_smile: