HTTP Service
Contents
HTTP Services are deprecated and will be removed in future releases. It is replaced by Universal APIs.
It is possible to control its availability via legacy.httpService.enabled parameter in com.enonic.xp.portal.cfg
HTTP Service enable mouting of http controllers safely made available within an application (or site’s) url space, without the use of routers.
Controller
To create a service, place an http controller file in the src/main/resources/services/ structure of your project. Each controller needs to be placed in a folder matching it’s name i.e.: src/main/resources/services/myservice/myservice.js
Example service controller
exports.GET = function(req) {
return {
body: {
time: new Date()
},
contentType: 'application/json'
};
};
Endpoint
Services use the following mount pattern: **/_/service/<appname>/<servicename>. This means that a service can be invoked in multiple locations, i.e. domain.com/_/service/.. or domain.com/some/path/_/service/... The context the service is mounted on can also be used by the controller as context.
As an example the service 'profileimage' will only work in context of a profile’s url. I.e. domain.com/profile/user1/_/service/myapp/profileimage will service an image, but invoked elsewhere it will not work.
Descriptor
By default, services may by used by any client or user. At times, it makes sense to restrict access to the service based on user roles. This can be done without custom coding.
Sample descriptor limiting access to the service:
<service>
<allow>
<principal>role:system.admin</principal>
<principal>role:myapp.myrole</principal>
</allow>
</service>
serviceUrl()
To safely generate a service URL, use the serviceUrl() function that is part of the Portal Library.
| When invoked, the function will generate a contextual service url based on the context of the current controller. |