Setting the Request Message Body
Setting the Request Message Body for a Network Request Body using Hypertext Transfer Protocol (HTTP)Query Strings
When there is additional user data to be sent to the server it is either added to the Uniform Resource Locator (URL) when using the GET Method, or added in the Request Message Body when using the POST Method. The data being sent is contained in what is referred to as the Query String, which contains data submitted in name value pairs (fields) with the name and value separated by an equal sign (=) and multiple fields separated by an ampersand (&). (Any whitespace in the value is replaced with a plus sign (+)). For example if we had the following three fields:
name: John Smith
age: 15
student: true
Then the query string would be formatted as shown below :
name=John+Smith&age=15&student=true
Setting the Request Message Body Using Text Data
In order to format multiple fields of text data (key name: value pairs) we will use the "Libraries.Network.NetworkRequest" and "Libraries.Containers.HashTable" libraries. We will then create multiple text type variables to store the key names and their associated value as shown below:
use Libraries.Network.NetworkRequest
use Libraries.Containers.HashTable
class Main
action Main
text key1 = "name"
text value1 = "John Smith"
text key2 = "age"
text value2 = "15"
text key3 = "student"
text value3 = "true"
Next, we will create a HashTable object "data" to add the name:value pairs to. Then we will create a NetworkRequest object "request" which will contain the formatted query string with "data’s" name:value pairs.
//creates a HashTable object "data" and adds the key name:value pairs to
//"data"
HashTable<text, text> data
data:Add(key1, value1)
data:Add(key2, value2)
data:Add(key3, value3)
//creates a NetworkRequest object "request" and formats data’s name:value
//pairs as a query string for the Request Message Body
NetworkRequest request
request:SetParameters(data)
Finally, we will output the formatted query string to the screen. The complete code section looks like this:
use Libraries.Network.NetworkRequest
use Libraries.Containers.HashTable
class Main
action Main
text key1 = "name"
text value1 = "John Smith"
text key2 = "age"
text value2 = "15"
text key3 = "student"
text value3 = "true"
HashTable<text, text> data
data:Add(key1, value1)
data:Add(key2, value2)
data:Add(key3, value3)
NetworkRequest request
request:SetParameters(data)
output request:GetBody()
end
end
The output should look similar to this, with each key name and value separated by an equal "=" sign (the order may be different from the example output shown below, which will not matter as the values are accessed by their key name and not by their order):
name=John+Smith&age=15&student=true
The output should look similar to this, with each key name and value separated by an equal "=" sign (the order may be different from the example output shown below, which will not matter as the values are accessed by their key name and not by their order):
name=Jane+Adams&age=22&student=false
Setting the Request Message Body Using JavaScript Object Notation (JSON) Data
In order to format JavaScriptObjectNotation object data ({"name":"value"} pairs) we will need to use the additional "Libraries.Data.Formats.JavaScriptObjectNotation" library. We will then create a JavaScriptObjectNotation object "data" which will contain the {"name":"value"} pairs as shown below: (For more information on JavaScriptObjectNotation see the Data Tutorials Section)
use Libraries.Network.NetworkRequest
use Libraries.Data.Formats.JavaScriptObjectNotation
class Main
action Main
text key1 = "name"
text value1 = "Jane Adams"
text key2 = "age"
integer value2 = 22
text key3 = "student"
boolean value3 = false
//creates a JavaScriptObjectNotation object "data"which will
//hold the (key name:value) pairs
JavaScriptObjectNotation data
data:Add(key1, value1)
data:Add(key2, value2)
data:Add(key3, value3)
We will then create a NetworkRequest object "request", format the JavaScriptObjectNotation object "data" as a query string, and output the formatted query string to the screen.
//creates a NetworkRequest object "request"
NetworkRequest request
//formats the JavaScriptObjectNotation object "data" as a query
//string for the Request Message Body
request:SetParameters(data)
//outputs the formatted query string to the screen
output request:GetBody()
Here is the complete code section to create a formatted query string from a JavaScriptObjectNotation object:
use Libraries.Network.NetworkRequest
use Libraries.Data.Formats.JavaScriptObjectNotation
class Main
action Main
text key1 = "name"
text value1 = "Jane Adams"
text key2 = "age"
integer value2 = 22
text key3 = "student"
boolean value3 = false
JavaScriptObjectNotation data
data:Add(key1, value1)
data:Add(key2, value2)
data:Add(key3, value3)
NetworkRequest request
request:SetParameters(data)
output request:GetBody()
end
end
Next Tutorial
In the next tutorial, we will discuss Managing a Network Connection Overview, which describes an overview into managing a network connection..