I Rest Web Services Server3 PDF
I Rest Web Services Server3 PDF
Introduction
For several years now, IBM i users have had the ability to deploy ILE programs and services
programs as web services based on the SOAP protocol using the integrated web services
server support that is part of the operating system. REST web services was not supported by the
integrated web services server, until now.
This article is the third in a series of articles about the integrated web services server REST
support.
• Part one starts out by explaining the basic concepts behind REST web services and how the
integrated web services server supports REST services.
• Part two takes you through the steps of deploying a simple ILE application as a RESTful web
service.
• In part three, we take you through the steps of deploying a more complex ILE application that
uses more of the REST features.
Prerequisites
Software
In order to get all the PTFs required by the integrated web services server in support of REST, you
need to load the latest HTTP Group PTF. Table 1 lists the HTTP group PTFs that are needed for
each of the supported releases of the IBM i operating system.
Assumptions
Before reading this article, you should have read Part one of the article in the series in order to
have a basic understanding of REST principles and the terminology used.
You can find links to various information on REST in the Resources section. You should also be
familiar with the fundamental concepts of JavaScript Object Notation (JSON) and XML.
SRA consists of a service program, STUDENTRSC, that contain the RESTful services (that is,
procedures) that provides the basic Create-Read-Update-Delete (CRUD) database logic, and a
database file, STUDENTDB, where student records are stored.
Listing 1 shows a partial listing of the source for the application that shows the prototypes of the
procedures to be exported from the STUDENTRSC service program (the RESTful resource).
Building a REST service with integrated web services server for Page 2 of 22
IBM i, Part 3
ibm.com/developerWorks/ developerWorks®
D getAll PR
D students_...
D LENGTH 10i 0
D students likeds(studentRec) dim(1000)
D options(*varsize)
D httpHeaders 100a dim(10)
D getByID PR
D studentID 9a const
D student likeds(studentRec)
D httpStatus 10i 0
D httpHeaders 100a dim(10)
D create PR
D student likeds(studentRec)
D httpStatus 10i 0
D httpHeaders 100a dim(10)
D update PR
D student likeds(studentRec)
D httpStatus 10i 0
D remove PR
D studentID 9a const
D httpStatus 10i 0
You should also note that a data structure template (studentRec) defines the fields in the database
file. You can optionally define the data structure using an externally described data structure, but
the decision to not use an externally defined data structure is due to wanting to control the case
of the XML element names and/or the JSON field names, which are obtained from the generated
program interface information. Previously, element and field names were all capitalized. However,
a new feature was introduced and fixed in IBM i 7.1 (PTF SI55340) and IBM i 7.2 (PTF SI55442)
that allows you to control the case by specifying the *DCLCASE parameter for the PGMINFO
control specification keyword. When specified, the names in the program interface information will
be generated in the same case as the names defined in the RPG source file.
Building a REST service with integrated web services server for Page 3 of 22
IBM i, Part 3
developerWorks® ibm.com/developerWorks/
In the following sections, go through these basic questions in the context of the application that will
be deployed. Table 2 shows a summary of the mappings that we want between the HTTP methods
and the URIs for the SRA application.
Note: The default context-root for an integrated web services server is /web/services.
openStudentDB();
read(e) studentR;
if (%ERROR);
httpStatus = H_SERVERERROR;
return;
endif;
read(e) studentR;
if (%ERROR);
httpStatus = H_SERVERERROR;
return;
endif;
enddo;
Building a REST service with integrated web services server for Page 4 of 22
IBM i, Part 3
ibm.com/developerWorks/ developerWorks®
httpStatus = H_OK;
httpHeaders(1) = 'Cache-Control: no-cache, no-store';
closeStudentDB();
/END-FREE
P getAll E
Notice that in the parameter list, there is an array that contains the student records and a
corresponding length field that indicates the number of student registration records in the array. If
you do not specify the length field, the response will include empty elements which would degrade
performance in both the client and the server.
The httpStatus parameter is used for the HTTP status code that is returned to the client. On
unexpected errors, the HTTP status code H_SERVERERROR (500) is returned. Otherwise, H_OK (200)
is returned.
The httpHeaders parameter is used to return the HTTP headers. In this example, we do not want
the response cached and thus the Cache-Control HTTP header is set.
openStudentDB();
httpStatus = H_OK;
else;
httpStatus = H_NOTFOUND;
endif;
closeStudentDB();
/END-FREE
P getByID E
Building a REST service with integrated web services server for Page 5 of 22
IBM i, Part 3
developerWorks® ibm.com/developerWorks/
The studentID parameter is an input parameter that is used as a key to read the student
registration from the database. If not found, the HTTP status code H_NOTFOUND (404) is returned.
The caching control HTTP header is also set so the response is not cached.
studentID = student.studentID;
firstName = student.firstName;
lastName = student.lastName;
gender = student.gender;
write(e) studentR;
if NOT %ERROR;
httpStatus = H_CREATED;
httpHeaders(1) = 'LOCATION: ' + 'http://server/web/service/students/' + studentID;
elseif %STATUS = ERR_DUPLICATE_WRITE;
httpStatus = H_CONFLICT;
else;
httpStatus = H_SERVERERROR;
endif;
closeStudentDB();
/END-FREE
P create E
If the student ID already exists in the database, an HTTP status code of H_CONFLICT (409) is
returned. An HTTP status code of H_CREATED (201) is returned when the new registration is
successfully created. For HTTP 201 status code responses, the Location HTTP is returned and is
set to the URI of the new resource that was created by the request.
Building a REST service with integrated web services server for Page 6 of 22
IBM i, Part 3
ibm.com/developerWorks/ developerWorks®
update(e) studentR;
if NOT %ERROR;
httpStatus = H_NOCONTENT;
else;
httpStatus = H_NOTFOUND;
endif;
else;
httpStatus = H_NOTFOUND;
endif;
closeStudentDB();
/END-FREE
P update E
If the student ID is not found, an HTTP status code of H_NOTFOUND (404) is returned. On a
successful update operation, the HTTP status code of H_NOCONTENT (204) is retuned because the
procedure does not return any data.
closeStudentDB();
/END-FREE
P remove E
Building a REST service with integrated web services server for Page 7 of 22
IBM i, Part 3
developerWorks® ibm.com/developerWorks/
If the student ID is not found, an HTTP status code of H_NOTFOUND (404) is returned. On a
successful delete operation, the HTTP status code of H_NOCONTENT (204) is returned because the
procedure does not return any data.
If you want to re-create the steps on your server, the source of the SRA application is available in
the Download section.
To populate the table with sample student registration data, run the following SQL command:
INSERT INTO STUDENTRSC/STUDENTDB
(studentID, firstName, lastName, gender)
VALUES('823M934LA', 'Nadir', 'Amra', 'Male'),
('826M660CF', 'John', 'Doe', 'Male'),
('747F023ZX', 'Jane', 'Amra', 'Female')
Building a REST service with integrated web services server for Page 8 of 22
IBM i, Part 3
ibm.com/developerWorks/ developerWorks®
On the Deploy New Service page, you can either deploy a SOAP or a REST web service (see
Figure 1). Because we are deploying a REST web service, we have selected the REST option.
Click Next.
Building a REST service with integrated web services server for Page 9 of 22
IBM i, Part 3
developerWorks® ibm.com/developerWorks/
Fill in library name as studentsrc and ILE Object name of studentsrc and click Next.
The resource name has been changed to students. In addition, you have the ability to set a URI
path template for the resource. For this example, we do not need to specify anything because the
path to the resource after changing the resource name is:
/context-root/students
Building a REST service with integrated web services server for Page 10 of 22
IBM i, Part 3
ibm.com/developerWorks/ developerWorks®
The parameter attributes are modifiable. In most cases, you want to modify the parameter
attributes to control what data is to be sent by web service clients and what data is to be returned
in the responses to the client requests.
In Figure 4, the REMOVE procedure is the procedure that is to be used to remove a student
registration. It has two parameters, studentID and httpStatus. The studentID parameter is the
identifier of the student to be removed and thus is an input parameter to the procedure. The
httpStatus parameter is the HTTP status code to be returned in response to the client and is
designated as an output parameter.
If you scroll down the Export procedures section you can see the UPDATE, CREATE, and GETBYID
procedures (see Figure 5). The parameters have been designated as input or output parameters.
Building a REST service with integrated web services server for Page 11 of 22
IBM i, Part 3
developerWorks® ibm.com/developerWorks/
Scrolling down a little bit more, you can find the GETALL procedure (see Figure 6) that returns all
the student registration data in the database.
If we provide values as shown in Figure 6, then a request handled by the procedure returns
a response that contains 1000 student records in which 3 student records contain student
registration data, and 997 student records will be empty. Because we want to only return the
actual number of registered students, the count column for the students parameter is changed
to reference the students_LENGTH parameter (see Figure 7). This parameter is set by the GETALL
procedure to the actual number of registered students, resulting in the response containing data
for only the actual number of registered students.
Building a REST service with integrated web services server for Page 12 of 22
IBM i, Part 3
ibm.com/developerWorks/ developerWorks®
Click Next.
Method DELETE
Method PUT
Method POST
409 Conflict
Method GET
Building a REST service with integrated web services server for Page 13 of 22
IBM i, Part 3
developerWorks® ibm.com/developerWorks/
Method GET
Building a REST service with integrated web services server for Page 14 of 22
IBM i, Part 3
ibm.com/developerWorks/ developerWorks®
Building a REST service with integrated web services server for Page 15 of 22
IBM i, Part 3
developerWorks® ibm.com/developerWorks/
Building a REST service with integrated web services server for Page 16 of 22
IBM i, Part 3
ibm.com/developerWorks/ developerWorks®
Building a REST service with integrated web services server for Page 17 of 22
IBM i, Part 3
developerWorks® ibm.com/developerWorks/
In order for the web service to run correctly, the user ID status must be set to *ENABLED and the
password must be set to a value other than *NONE. If a user ID is specified that is disabled or has
a password of *NONE, a warning message is displayed and the service might not run correctly. In
addition, ensure that the specified user ID has the proper authorities to any resources and objects
that the program object needs, such as libraries, databases, and files.
You have the option of placing the libraries at the start of the user portion of the library list or at the
end of the user portion of the library list. Click Next.
Building a REST service with integrated web services server for Page 18 of 22
IBM i, Part 3
ibm.com/developerWorks/ developerWorks®
HTTP headers indicate what transport headers (for example, HTTP headers) to pass to the web
service implementation code. Transport headers are passed as environment variables. The
environment variable name for HTTP headers is made up of the specified HTTP header prefixed
with 'HTTP_', all in upper case. For example, if 'Content-type' is specified, then the environment
variable name would be 'HTTP_CONTENT-TYPE'. If an HTTP header was not passed to the web
service request, the environment variable value will be set to the null string.
Click Next.
Building a REST service with integrated web services server for Page 19 of 22
IBM i, Part 3
developerWorks® ibm.com/developerWorks/
Clicking Finish at the bottom of the summary page will begin the installation process. When the
web service is deployed, the deployed service becomes active (see the green dot to the left of the
service name) as in Figure 17.
Congratulations, you have now successfully deployed an ILE program object as a RESTful web
service.
Unlike SOAP, there is no test client provided by the IBM Web Administration GUI to test the REST
service. Resource methods that are bound to the HTTP GET method could be tested by using a
browser. Figure 18 shows the result of the request that returns all registered students.
Building a REST service with integrated web services server for Page 20 of 22
IBM i, Part 3
ibm.com/developerWorks/ developerWorks®
Figure 19 shows the result of a request for a student record with a student ID of 823M934LA.
To test the other resource methods, an external tool (such as SoapUI) must be used, Figure 20
shows the result of a request to create a new student registration using SoapUI.
Looking at Figure 20, because we are attempting to create a new student registration the HTTP
method is POST (1). The sub panel numbered (2) is the new student registration data in the
JSON format that will be sent to the server as part of the HTTP POST request. After submitting
the request, that server response did not return any JSON data (3). Because the create request
succeeded, the REST service returned the HTTP status code of 201 (Created) and a location
header that contains the URL of the newly created student registration resource (4).
Summary
In Part one of this series, you learned the basic concepts behind REST web services and how the
integrated web services server supports REST services. In Part two of this series, you learned how
to deploy a simple ILE application as a RESTful web service. In this article, you learned how to
deploy a more complex ILE application that uses more of the REST features.
The integrated web services server REST support provides a solid foundation for creating and
deploying REST APIs based on ILE programs or service programs on the IBM i platform. Add
the highly intuitive IBM Web Administration for i GUI for deploying web services, and you've got
everything you need to quickly prototype and deploy your own custom REST API. So what are you
waiting for?
Building a REST service with integrated web services server for Page 21 of 22
IBM i, Part 3
developerWorks® ibm.com/developerWorks/
Download
Description Name Size
Resources
• For everything about the integrated Web services support on IBM i see the product web page.
• Part one starts out by explaining the basic concepts behind REST web services and how the
integrated web services server supports REST services.
• Part two takes you through the steps of deploying a simple ILE application as a RESTful web
service.
• See RESTful Web services: The basics to learn what REST is all about.
• See developerWorks: Learn about REST for articles, tutorials, standards, and other technical
resources about REST web services.
• For information about the HTTP protocol and content negotiation, see RFC 2616.
• For information about Java regular expressions, see the Java Tutorials provided lesson on
regular expressions.
Building a REST service with integrated web services server for Page 22 of 22
IBM i, Part 3