Transformation not working on v3.2.1

I am unable to transform json request body. I am on v3.2.1
The app definition is below.

I don’t think it matters but I am using .NET 5 api. There is no change in the request body using both file or base64 string. I also created templates in a template folder adjacent to apps as well as in root. The app is being loaded and I can tell the routing is correct (modified to wrong route and it won’t hit the endpoint).


    "name": "Members API",
    "api_id": "1",
    "org_id": "default",
    "auth_configs": {
      "authToken": {
        "auth_header_name": "Authorization"
      }
    },
    "definition": {
        "location": "header",
        "key": "x-api-version"
    },
    "version_data": {
      "not_versioned": true,
      "versions": {
        "Default": {
          "name": "Default",
          "use_extended_paths": true,
          "paths": {
            "ignored": [],
            "white_list": [],
            "black_list": []
         },
          "extended_paths": {
            "transform": [
                {
                    "path": "members",
                    "method": "PUT",
                    "template_data": {
                      "template_mode": "blob",
                      "template_source": "ew0KIklEIjp7e2luZGV4IC4gInJlc3VsdCIgImlkIn19LA0KIklEQm9vayI6e3tpbmRleCAuICJyZXN1bHQiICJib29rIn19LA0KIkZpcnN0TmFtZSI6Int7aW5kZXggLiAicmVzdWx0IiAiRmlyc3ROYW1lIn19IiwNCiJMYXN0TmFtZSI6Int7aW5kZXggLiAicmVzdWx0IiAiTGFzdE5hbWUifX0iDQp9",
                      "input_type": "xml",
                      "enable_session": false
                    }
                },
                {
                    "path": "/members",
                    "method": "POST",
                    "template_data": {
                      "template_mode": "blob",
                      "template_source": "ew0KICAidmFsdWUxIjogInt7LnZhbHVlMn19IiwNCiAgInZhbHVlMiI6ICJ7ey52YWx1ZTF9fSIsDQogICJ0cmFuc2Zvcm1lZF9saXN0IjogWw0KICAgIHt7cmFuZ2UgJGluZGV4LCAkZWxlbWVudCA6PSAudmFsdWVfbGlzdH19DQogICAgICAgIHt7aWYgJGluZGV4fX0NCiAgICAgICAgLCAie3skZWxlbWVudH19Ig0KICAgICAgICB7e2Vsc2V9fQ0KICAgICAgICAgICJ7eyRlbGVtZW50fX0iDQogICAgICAgIHt7ZW5kfX0NCiAgICB7e2VuZH19DQogIF0NCn0=",
                      "input_type": "json",
                      "enable_session": false
                    }
                },
                {
                    "path": "/members/{id}",
                    "method": "POST",
                    "template_data": {
                      "template_mode": "file",
                      "template_source": "./templates/transform_test.tmpl",
                      "input_type": "json",
                      "enable_session": false
                    }
                }
            ]
          }
          
      }
     }
    },
    "proxy": {
      "preserve_host_header": false,
      "listen_path": "/members/",
      "target_url": "http://host.docker.internal:5000/api/members/",
      "strip_listen_path": true
    },
    "response_processors": [
        {
            "name": "response_body_transform",
            "options": {}
        }
    ],
    "active": true
  }

(Additionally, I tried to transform xml request to json with transformation that creates json. But this is being ignored and xml is being sent to the api resulting in Http 415 Unsupported. Please note that this isn’t the main issue)

This is the request body.

{
  "value1": "value-1",
  "value2": "value-2",
  "value_list": [
    "one",
    "two",
		"four",
    "three"
  ]
}

And this is the template.

{
  "value1": "{{.value2}}",
  "value2": "{{.value1}}",
  "transformed_list": [
    {{range $index, $element := .value_list}}
        {{if $index}}
        , "{{$element}}"
        {{else}}
          "{{$element}}"
        {{end}}
    {{end}}
  ]
}

Also tried with two others versions of tykio/tyk-gateway (v4.0rc21 and v3.0.4). Same result. There is no transformation.
Additional headers:

Content-Type: application/json
Authorization: default9f1f66b1404c4e95b8d44cf35b7d9074

That token is created with POST call to Create Tyk Token which sends body as below.

{
  "quota_max": 0,
  "rate": 2,
  "per": 5,
  "org_id": "default",
  "access_rights": {
      "1": {
          "api_name": "Tyk Test API",
          "api_id": "1",
          "versions": [
              "Default"
          ],
          "allowed_urls": [],
          "limit": null,
          "allowance_scope": ""
      }
    }
}

Hi @tej-rana, I am checking and would get back to you

1 Like

Hi @tej-rana, I made some edits to your API definition (target_url and use_kesyless) and I got it to work on v3.2.1. Can you let us know how you are making the call?

The path for your transformation should be http://host.docker.internal:5000/api/members/members. The path inside the transform is a Subpath.

1 Like

Yes that was it.

So I updated the listen_path and target_url keeping the rest as is and it transformed. So both file and blob worked.

 "listen_path": "/api/",
        "target_url": "http://host.docker.internal:5000/api/",

Thank you for pointing me to it.

Is there a need for ordering of the path so “/members” is listed below “/members/action/123”? I added a new template to map to a more specific path but it seemed to want to take the template for the generic ("/members") path.

But after playing around a bit it seemed to not care about the order. It is bit fiddly.

It also seems to me as blob source is more reliable than file source. When I added a new template file it was not doing the transformation but worked immediately upon changing to blob.

Is there a need for ordering of the path so “/members” is listed below “/members/action/123”? I added a new template to map to a more specific path but it seemed to want to take the template for the generic ("/members") path.

I think the order maybe necessary. I’ll confirm from internally and get back to you. Could you share the order in which it worked and the order in which it failed

It also seems to me as blob source is more reliable than file source. When I added a new template file it was not doing the transformation but worked immediately upon changing to blob.

For file mode, you need to ensure the file is copied in the directory. For example, you would need to restart (and not hot reload) the gateway if you were using docker volumes to map the directories.