Request Transformation not working

Here i am using this api : http://fakerestapi.azurewebsites.net/swagger/ui/index#!/Authors/Authors_Post

in this i am using api: POST /api/Authors
in this whatever i am passing in json body it respond me back same result in json

so in this when i am passing in request body

request body

{
  "ID": 20,
  "IDBook": 501,
  "FirstName": "Nikhil",
  "LastName": "Purohit"
}

and i will get same response as

{
  "ID": 20,
  "IDBook": 501,
  "FirstName": "Nikhil",
  "LastName": "Purohit"
}

So now what i want is now i send request body as xml and by using tyk cloud request body transformation i convert this into json and get result. overall i want to use request body transformation from xml to json.

Now i am passing xml in request body as

<?xml version="1.0" encoding="utf-8"?>
<result>
<id>5122</id>
<book>62350</book>
<FirstName>Nikhil</FirstName>
<LastName>Purohit</LastName>
</result>

and in request body tranformation my template is

{
"ID":{{index . "result" "id"}},
"IDBook":{{index . "result" "book"}},
"FirstName":"{{index . "result" "FirstName"}}",
"LastName":"{{index . "result" "LastName"}}"
}

screenshot of my tyk cloud configuration
request tranformation

and in result i am getting error

{
    "error": "There was a problem proxying the request"
}

bad result again

help me to fix this.

here is my tyk definition file :

{
    "id": "5aa2155b345389000160e7ae",
    "name": "fake-api-test",
    "slug": "fake-api-test",
    "api_id": "97d0d8c118fb4b1d68d29c1a1d274158",
    "org_id": "5a9d016e3602190001f44512",
    "use_keyless": true,
    "use_oauth2": false,
    "use_openid": false,
    "openid_options": {
        "providers": [],
        "segregate_by_client": false
    },
    "oauth_meta": {
        "allowed_access_types": [],
        "allowed_authorize_types": [],
        "auth_login_redirect": ""
    },
    "auth": {
        "use_param": false,
        "param_name": "",
        "use_cookie": false,
        "cookie_name": "",
        "auth_header_name": "",
        "use_certificate": false
    },
    "use_basic_auth": false,
    "use_mutual_tls_auth": false,
    "client_certificates": [],
    "upstream_certificates": {},
    "enable_jwt": false,
    "use_standard_auth": false,
    "enable_coprocess_auth": false,
    "jwt_signing_method": "",
    "jwt_source": "",
    "jwt_identity_base_field": "",
    "jwt_client_base_field": "",
    "jwt_policy_field_name": "",
    "notifications": {
        "shared_secret": "",
        "oauth_on_keychange_url": ""
    },
    "enable_signature_checking": false,
    "hmac_allowed_clock_skew": -1,
    "base_identity_provided_by": "",
    "definition": {
        "location": "header",
        "key": "x-api-version"
    },
    "version_data": {
        "not_versioned": true,
        "default_version": "",
        "versions": {
            "Default": {
                "name": "Default",
                "expires": "",
                "paths": {
                    "ignored": [],
                    "white_list": [],
                    "black_list": []
                },
                "use_extended_paths": true,
                "extended_paths": {
                    "transform": [
                        {
                            "template_data": {
                                "input_type": "xml",
                                "template_mode": "blob",
                                "enable_session": false,
                                "template_source": "ewoiSUQiOnt7aW5kZXggLiAicmVzdWx0IiAiaWQifX0sCiJJREJvb2siOnt7aW5kZXggLiAicmVzdWx0IiAiYm9vayJ9fSwKIkZpcnN0TmFtZSI6Int7aW5kZXggLiAicmVzdWx0IiAiRmlyc3ROYW1lIn19LAoiTGFzdE5hbWUiOiJ7e2luZGV4IC4gInJlc3VsdCIgIkxhc3ROYW1lIn19Igp9"
                            },
                            "path": "Authors",
                            "method": "POST"
                        }
                    ]
                },
                "global_headers": {},
                "global_headers_remove": [],
                "global_size_limit": 0,
                "override_target": ""
            }
        }
    },
    "uptime_tests": {
        "check_list": [],
        "config": {
            "expire_utime_after": 0,
            "service_discovery": {
                "use_discovery_service": false,
                "query_endpoint": "",
                "use_nested_query": false,
                "parent_data_path": "",
                "data_path": "",
                "port_data_path": "",
                "target_path": "",
                "use_target_list": false,
                "cache_timeout": 60,
                "endpoint_returns_list": false
            },
            "recheck_wait": 0
        }
    },
    "proxy": {
        "preserve_host_header": false,
        "listen_path": "/97d0d8c118fb4b1d68d29c1a1d274158/",
        "target_url": "http://fakerestapi.azurewebsites.net/api/",
        "strip_listen_path": true,
        "enable_load_balancing": false,
        "target_list": [],
        "check_host_against_uptime_tests": false,
        "service_discovery": {
            "use_discovery_service": false,
            "query_endpoint": "",
            "use_nested_query": false,
            "parent_data_path": "",
            "data_path": "",
            "port_data_path": "",
            "target_path": "",
            "use_target_list": false,
            "cache_timeout": 0,
            "endpoint_returns_list": false
        }
    },
    "disable_rate_limit": false,
    "disable_quota": false,
    "custom_middleware": {
        "pre": [],
        "post": [],
        "post_key_auth": [],
        "auth_check": {
            "name": "",
            "path": "",
            "require_session": false
        },
        "response": [],
        "driver": "",
        "id_extractor": {
            "extract_from": "",
            "extract_with": "",
            "extractor_config": {}
        }
    },
    "custom_middleware_bundle": "",
    "cache_options": {
        "cache_timeout": 60,
        "enable_cache": true,
        "cache_all_safe_requests": false,
        "cache_response_codes": [],
        "enable_upstream_cache_control": false,
        "cache_control_ttl_header": ""
    },
    "session_lifetime": 0,
    "active": true,
    "auth_provider": {
        "name": "",
        "storage_engine": "",
        "meta": {}
    },
    "session_provider": {
        "name": "",
        "storage_engine": "",
        "meta": {}
    },
    "event_handlers": {
        "events": {}
    },
    "enable_batch_request_support": false,
    "enable_ip_whitelisting": false,
    "allowed_ips": [],
    "dont_set_quota_on_create": false,
    "expire_analytics_after": 0,
    "response_processors": [
        {
            "name": "response_body_transform",
            "options": {}
        }
    ],
    "CORS": {
        "enable": false,
        "allowed_origins": [],
        "allowed_methods": [],
        "allowed_headers": [],
        "exposed_headers": [],
        "allow_credentials": false,
        "max_age": 24,
        "options_passthrough": false,
        "debug": false
    },
    "domain": "",
    "do_not_track": false,
    "tags": [],
    "enable_context_vars": false,
    "config_data": {},
    "tag_headers": [],
    "global_rate_limit": {
        "rate": 0,
        "per": 0
    },
    "strip_auth_data": false
}

Thanks in advance
Nikhil Purohit

Hi

If you append <br /> to the end of your XML in the request body you should find that it works. I also added the modify headers middleware to add two request headers:

Accept: application/json
Content-Type: application/json

Thanks
Josh

1 Like

Thanks Josh, now its working fine :slightly_smiling_face:

I know this is already closed but I can’t transform json request body. I am on v3.2.1
The app definition is below.

(Additionally, I tried borrowing the logic (to transform) to test xml from here but the request is sent as xml instead of json.)

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
  }

Hi Tej-rana,
Can you provide the json object that you want to transform ?

All good. The problem was in target_url. I changed that to be ```
http://host.docker.internal:5000/api/