Inject JSON Body

Can we inject JSON Body in the request explicitly.

The use case I have is, have to inject inject json body explicitly where in the incoming request no body is present.

Try using Transform plugin feature. You can either use a file template mode as shown in our doc or the blob template mode as shown below.

"transform": [
                "template_data": {
                  "input_type": "json",
                  "template_mode": "blob",
                  "enable_session": false,
                  "template_source": "ewogICAgInN0cmluZyI6ICJUaGlzIGlzIGEganNvbiBzdHJpbmchIiwKICAgICJpbnQiOiAxMgp9",
                  "input": "",
                  "output": ""
                "path": "widgets/{id}",
                "method": "POST"

The value of the template_source is simply a 64 bit encoded value of the template. You can copy the value below and use a base64 decoder to retrieve the json.

As I am not getting body in the incoming request and I want TYK to inject some valid JSON DATA ( even valid format (empty json) like {} is also fine).

As per the suggestion I have enabled below settings in the API definition:

“transform”: [
“path”: “/”,
“method”: “GET”,
“template_data”: {
“template_mode”: “file”,
“template_source”: “./templates/test.tmpl”,
“input_type”: “json”,
“enable_session”: true,

I have configured test.tmpl file as below:



I feel the template file is wrong. I just want to inject some valid json data. Could you Please suggest how I can achieve. Please do note that there is no body present in the coming request. So we want TYK to inject that explicitly.

Please help with above query.

Could you try using the blob (base 64 encoded) instead. I have tried this and it works.

Seems like the file based template source does not work for an empty body. I have tested this in version 3.0.0. I will check with the internal team regarding this. Can you confirm your version of the gateway so I can test as well.

Tested with version 3.2.2 and file based method works. What is your version of the gateway?

My gateway version is 3.2. Can you Please suggest how to integrate it using blob.

How do we specify empty template using blob?

Integrating with the blob method is very easy. You need to encode the JSON you wish to inject into base 64 and add it as the value for template_source. For example you can decode the sample I shared with you earlier to extract the JSON.

File based method works for your version which is v3.2. It also works in the latest v3.0 release. I noticed v3.0.0 I was using was a year old.

If you still want to use the file based method, then you need to ensure you have the file (transform_test.tmpl) in your templates folder. The templates folder is at the root of your Tyk directory.

# ls
apps   coprocess       install     plugins   templates  tyk.conf
certs  event_handlers  middleware  policies  tyk

# ls templates
error.json  error.xml  transform_test.tmpl

You can follow out github page to recreate the template folder. Hope this helps.