When performing an HTTP POST to a web script, the posted request body often contains
content that needs processing by the web script. To allow access to the request body, the Web
Script Framework provides a special root object named requestbody that
represents the content of the request. The requestbody is a
ScriptContent object allowing access to the request content either as a
string or as a content stream.
This task demonstrates request processing by creating a web script, which simply
responds with the content of the HTTP request.
-
Log in to SkyVault Share:
- Open a web browser and enter the URL: http://localhost:8080/share
- If prompted, log in with the user name admin and password admin.
- Navigate to Data Dictionary > Web Scripts Extensions > org > example.
-
Create a web script description document for your request body sample:
- In the Create menu, select XML.
- Enter the name for the web script in the Name field: requestbody.post.desc.xml
-
Type the following in the content box:
<webscript> <shortname>Request Body Sample</shortname> <description>Render the request body in the response</description> <url>/requestbody</url> <authentication>user</authentication> </webscript>
- Click Create.
- Navigate back to the org/example folder using the breadcrumb trail.
-
Create a controller script for your request body sample:
- In the Create menu, select Plain Text.
- Enter the name in the Name field: requestbody.post.js
-
Type the following in the content box:
model.requestcontent = requestbody.content;
- Click Create.
- Navigate back to the org/example folder using the breadcrumb trail.
-
Create an HTML response template for your request body sample:
- In the Create menu, select Plain Text.
- Enter the name in the Name field: requestbody.post.html.ftl
-
Type the following in the content box:
${requestcontent}
- Click Create.
- Navigate back to the org/example folder using the breadcrumb trail.
-
Register the web scripts with SkyVault Content Services.
- In a new web browser tab, enter the URL: http://localhost:8080/alfresco/service/index
- If prompted, log in with the user name admin and password admin.
- Click Refresh Web Scripts.
A message indicates there is an additional web script.
Your example consists of just two lines of code. The controller script extracts the request content from the requestbody root object and places it into the Web script model under the name requestcontent. The response template simply outputs the model value into the response.
-
Test this web script with cURL by typing the following in your command line:
curl -uadmin:admin -H "Content-Type: application/json" --data-binary "{\"request\":\"body\"}" "http://localhost:8080/alfresco/service/requestbody"
This posts a request body of {"request": "body"} to your web script, which in turn responds with: {"request": "body"}
Often the content posted in a request is structured using data formats such as XML or
JSON, which the web script has to parse. Parser code is generally painful to develop, so the
Web Script Framework provides a mechanism known as a Format Reader that parses a request of a
given MIME type into an object that represents the request content. The object is then
supplied to the controller script, which can interrogate the object to extract request
content.