Reading responses
Reading responses
The dynamic functionality returns the entire api response json content back as special json object that we can manipulate with special commands to get back data.
Specifying the request to get the data for
If we had for example previously defined a request like this
<input type="hidden" name="PricingApi.Api.Get.myCall"
value="v1/price-lists"
data-preset-val="v1/price-lists">
Then we can print out the json content with the following syntax.
{{ .Data.PricingApi.Api.Requests.myCall.Response }}
The response object holds all the requests in it and can be used to reference the results back for any of them.
Response data
The response objects will contain
Json result
{{ .Data.PricingApi.Api.Requests.myRequest1.Response }}
Response http code (from 1.184+)
{{ .Data.PricingApi.Api.Requests.myRequest1.ResponseHttpCode }}
Check if the response had a specific http code
{{ if .Data.PricingApi.Api.Requests.myRequest1.ResponseHttpCode 204 }}
204 was indeed returned!
{{ end }}
Response headers (from 1.184+)
{{ .Data.PricingApi.Api.Requests.myRequest1.ResponseHeaders }}
Read specific response header content
{{ .Data.PricingApi.Api.Requests.myRequest1.ResponseHeaders.X-Some-Header }}
Get specific items
To get a specific field back from the response json content we can use the Get name.
Lets assume our api response data looks like this
[
{
"id": 1,
"name": "Test",
"start": "0000-00-00",
"end": "0000-00-00",
"isActive": 1
},
{
"id": 3,
"name": "Testing Update",
"start": "0000-00-00",
"end": "0000-00-00",
"isActive": 1
}
]
Our data is an array of elements, we can use a couple of different methods to read the keys.
Iterate the array
To iterate the through all items we would append the Response with Array, this tells the system that we are expecting an array of elements.
Each item in the array would correspond to the actual object, where we can use the Get function to fetch a specific field.
{{
<ul>
{{ range .Data.PricingApi.Api.Requests.myRequest1.Response.Array }}
<li>{{ .Get "id" }}</li>
{{ end }}
</ul>
}}
Get a specific field value
The Get function can be used to return any single value from the json data.
Get a value from a specific index in the array. This would return the first results id value. The number indicates the array index.
{{ .Data.PricingApi.Api.Requests.myCall.Response.Get "0.id" }}
This function can return multiple items based on the provided syntax
{
"name": {"first": "Tom", "last": "Anderson"},
"age":37,
"children": ["Sara","Alex","Jack"],
"fav.movie": "Deer Hunter",
"friends": [
{"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
{"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
{"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
]
}
{{ .Data.PricingApi.Api.Requests.myCall.Response.Get "name.last" }} => Anderson
{{ .Data.PricingApi.Api.Requests.myCall.Response.Get "age" }} => 37
{{ .Data.PricingApi.Api.Requests.myCall.Response.Get "children" }} => ["Sara","Alex","Jack"]
{{ .Data.PricingApi.Api.Requests.myCall.Response.Get "children.#" }} => 3
{{ .Data.PricingApi.Api.Requests.myCall.Response.Get "children.1" }} => Alex
{{ .Data.PricingApi.Api.Requests.myCall.Response.Get "child*.2" }} => jack
{{ .Data.PricingApi.Api.Requests.myCall.Response.Get "c?ildren.0" }} => Sara
{{ .Data.PricingApi.Api.Requests.myCall.Response.Get "fav\.movie" }} => Deer Hunter
{{ .Data.PricingApi.Api.Requests.myCall.Response.Get "friends.#.first" }} => ["Dale","Roger","Jane"]
{{ .Data.PricingApi.Api.Requests.myCall.Response.Get "friends.1.last" }} => Craig
Type differences
The json values by default have special type that is always converted to a string when used in the template.
This means that if the intention is to use these values in some inbuilt helper function that expects a certain type and the expected types are not what is expected then the function can break the template entirely.
To prevent this we can cast the values to the correct type in the functions.
{{ if eq (.Data.PricingApi.Api.Requests.myCall.Response.Get "age").Int }} 37 }}
<!-- Do something -->
{{ end }}
The following type based functions can be called
{{ (.Data.PricingApi.Api.Requests.myCall.Response.Get "val").Exists }} => bool
{{ (.Data.PricingApi.Api.Requests.myCall.Response.Get "val").Int }} => int64
{{ (.Data.PricingApi.Api.Requests.myCall.Response.Get "val").Float }} => float64
{{ (.Data.PricingApi.Api.Requests.myCall.Response.Get "val").String }} => string
{{ (.Data.PricingApi.Api.Requests.myCall.Response.Get "val").Bool }} => bool
Array query functionality
On array elements we can also use extra filtering options
If we used the sample below
[
{
"id": 1,
"name": "Test",
"start": "0000-00-00",
"end": "0000-00-00",
"isActive": 1
},
{
"id": 3,
"name": "Testing Update",
"start": "0000-00-00",
"end": "0000-00-00",
"isActive": 1
}
]
The following options are available
`#(name=="%s").id` "Test"` => 1
`#(name=="%s")#.id` "Test"` => [1]
`#(id>2)#.id` "Test"` => 1
Then in order to print only the name of the item that has id 3
<ul>
{{ $cs := .Data.CRMApi.Api.Requests.customer.Response.Array }}
<li>{{ $cs.Get (printf `#(id==%d)#.name` 3) }}</li>
{{ end }}
</ul>
Data manipulation functions
These are special functions that can be used against the dynamic api json response content, to do special conversions or other special functions with them.
Note that the following functions can also be used on the same result, for example we want to flatten and get unique values at the same time.
{{ .Data.ErplyApi.Api.Requests.getProducts.Response.Get "records.#.productId|@flatten|@unique" }}
- @commaSepStr - turn an array into comma separated string
If we had a json result like this
{
"records": [
{
"id": 1,
"name": "Test 1"
},
{
"id": 2,
"name": "Test 2"
}
]
}
Get the arrays specific values as comma separated string
{{ $commaSeparatedString := .Data.ErplyApi.Api.Requests.getProducts.Response.Get "records.#.id|@commaSepStr" }}
- @reverse - reorder an array in reverse order
If we had a json result like this
{
"records": [
{
"id": 1,
"name": "Test 1"
},
{
"id": 2,
"name": "Test 2"
}
]
}
We can use the function here to get the records array in reverse order
{{ $reversedRecords := .Data.ErplyApi.Api.Requests.getProducts.Response.Get "records@reverse" }}
We can get the last element in the array using this
{{ $lastRecord := .Data.ErplyApi.Api.Requests.getProducts.Response.Get "records@reverse.0" }}
- @keys - returns object field keys as an array
Using the same json sample above
Get the keys of the first record
{{ $keys := .Data.ErplyApi.Api.Requests.getProducts.Response.Get "records.0|@keys" }}
- @values - returns object field values as an array
Using the same json sample above
Get the values of the first record
{{ $values := .Data.ErplyApi.Api.Requests.getProducts.Response.Get "records.0|@values" }}
- @flatten - returns a flat array of the values
{
"records": [
{
"id": 1,
"rows": [
{"id": 3},
{"id": 4}
]
},
{
"id": 2,
"rows": [
{"id": 5},
{"id": 6}
]
}
]
}
Get a flat array of values
{{ $values := .Data.ErplyApi.Api.Requests.getProducts.Response.Get "records.#.rows.#.id|@flatten" }}
[3,4,5,6]
- @unique - returns an array of unique values only
{
"records": [
{
"productId": 1
},
{
"productId": 2
},
{
"productId": 1
}
]
}
Get the unique values
{{ $values := .Data.ErplyApi.Api.Requests.getProducts.Response.Get "records.#.productId|@unique" }}
[1,2]