You are here: Web Services > Consuming Web Services > Adding Contents to Your Request Body

Adding Contents to Your Request Body

Most non-GET requests would have some contents in the request body.

An example of information you want to include in the request body is the details of the new employee you are creating with your request.

In general, request body is just a sequence of bytes. In the context of web services, it's generally a text that encodes certain details. The text can be a URL-encoded or JSON representation of a set of values.

As mentioned in the previous sections, the Content property of the XPRIM_HttpRequest contains the following methods that you can use to add contents to your request body in the required format:

Refer to the limitations of using AddFile on IBM i.

Example - Creating a new Employee using a Web API

Let say that your company has a published API to create a new employee.

The URL looks like this:

http://yourcompany.com/api/hr/employee

Let's have a look at how you can add the details into the request body. The first example show how to add the details as URL-encoded form values, and the second example adds the content as a JSON string.

Adding the employee details as URL-encoded form values

Define_Com Class(#XPRIM_HttpRequest) Name(#Req)

#Req.Content.AddUrlEncodedFormValue Name('givenName') Value(#EmpGivenName)

#Req.Content.AddUrlEncodedFormValue Name('lastName') Value(#EmpLastName)​​

#Req.Content.AddUrlEncodedFormValue Name('address') Value(#EmpAddress)

* Execute the request (POST verb)

#Req.DoPost Url('http://yourcompany.com/api/hr/employee')

Adding the employee details as JSON

Define_Com Class(#XPRIM_HttpRequest) Name(#Req)

Define_Com Class(#XPRIM_JsonObject) Name(#JsonObject)

* Construct the JSON object

#JsonObject.InsertString('givenName' #EmpGivenName)

#JsonObject.InsertString('lastName' #EmpLastName)

#JsonObject.InsertString('address' #EmpAddress)

* Add the constructed JSON to the request body

#Req.Content.AddJsonObject(#JsonObject)

* Execute the request (POST verb)

#Req.DoPost Url('http://yourcompany.com/api/hr/employee')

Next: Constructing JSON Data for Your Request Body