Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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

 

XPRIM_HttpRequest

HttpClientRequest

AddFile

Yes

Yes

AddJson

Yes

Yes

AddJsonArray

Yes

Yes

AddJsonObject

Yes

Yes

AddString

Yes

Yes

AddUrlEncodedFormValue

Yes

Yes

AppendString

-

Yes

Refer to the limitations of using AddFile on IBM i.

Example - Creating a new Employee using a Web API

...

     Define_Com Class(#PRIM_SRVM.HttpClientRequest) 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

 

* Construct the JSON object

 

* Add the constructed JSON to the request body

 

* Execute the request (POST verb)

     Define_Com Class(#PRIM_SRVM.HttpClientRequest) Name(#Req)
    Define_Com Class(#PRIM_JSON.Document) Name(#Json)
Define_Com Class(#PRIM_JSON.Object) Name(#JsonObject) Reference(*DYNAMIC)

* Construct the JSON object

     #JsonObject <= #Json.CreateRootObject
    #JsonObject.AddStringMember Name('givenName') Value(#EmpGivenName)
    #JsonObject.AddStringMember Name('lastName') Value(#EmpLastName)
#JsonObject.AddStringMember Name('address') Value(#EmpAddress)

* Add the constructed JSON to the request body

     #Req.Content.AddJson Value(#Json)

* Execute the request (POST verb)

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

Next: Constructing JSON Data for Your Request Body