Constructing JSON Data for Your Request Body

V16 - HttpClientRequest

To efficiently construct and add a JSON object or array to your request body, use PRIM_JSON.Writer.

It's essential that you use PRIM_JSON.Writer when your JSON data is big (instead of directly instantiating multiple PRIM_JSON.Writer or PRIM_JSON.Array), as PRIM_JSON.Writer is designed to perform better for big data.

Define_Com Class(#PRIM_SRVM.HttpClientRequest) Name(#Req)

* Create a PRIM_JSON.Writer to efficiently write JSON data

Define_Com Class(#PRIM_JSON.Writer) Name(#JsonWriter) Textwriter(#StringWriter)

Define_Com Class(#PRIM_IOC.StringWriter) Name(#StringWriter)

* Start constructing the object

#JsonWriter.BeginObject

#JsonWriter.WriteString Membername('givenName') Value(#EmpGivenName)

#JsonWriter.WriteString Membername('lastName') Value(#EmpLastName)

#JsonWriter.WriteString Membername('address') Value(#EmpAddress)

#JsonWriter.EndObject

#Req.Content.AddString Value(#StringWriter.Text)

* Execute the request

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

V16 - HttpClientRequest

To efficiently construct and add a JSON object or array to your request body, use XPRIM_JsonWriter.

It's essential that you use XPRIM_JsonWriter when your JSON data is big (instead of directly instantiating multiple XPRIM_JsonObject or XPRIM_JsonArray), as XPRIM_JsonWriter is designed to perform better for big data.

Define_Com Class(#XPRIM_HttpRequest) Name(#Req)

* Create an XPRIM_JsonWriter to efficiently write JSON data

Define_Com Class(#XPRIM_JsonWriter) Name(#JsonWriter)

* Set the writer to output the JSON to the HTTP request object

#JsonWriter.SetOutputToHttpRequest HttpRequest(#Req)

* Start constructing the object

#JsonWriter.BeginObject#JsonWriter.WriteString Name('givenName') Value(#EmpGivenName)

#JsonWriter.WriteString Name('lastName') Value(#EmpLastName)

#JsonWriter.WriteString Name('address') Value(#EmpAddress)

#JsonWriter.EndObject

* Execute the request

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

Next: Creating Multiple Part (Multipart) Body