How to return dummy response from tyk API (with custom_middleware) without hitting back end. (TYK v4.3.4 opensource)

Hi
I am trying to create an API with custom_middleware in tyk which can return response without calling target url.
Is there any way to do that in tyk. If yes pls share the parameter to be added in api definition json for same.

Thanks!

Hi @Shubham_M_Mishra,

Welcome to the community :partying_face:

Please see here and here to use mock responses.
You’d want to use the white_list

Hi,

I used the white_list and it is working fine, I am getting the dummy response.

But, the custom middleware added to the definition is not working.

I need a way in which I am also able to run the middleware.

This is a sample api definition I am using:

{

"name": "TestAPI",

"api_id": "TestAPIforPOC",

"proxy": {

  "listen_path": "/api/test"

},

"version_data": {

    "not_versioned": true,

    "default_version": "",

    "versions": {

        "Default": {

            "name": "Default",

            "expires": "",

            "paths": {

                "ignored": [],

                "white_list": [],

                "black_list": []

            },

            "use_extended_paths": true,

            "global_headers": {},

            "global_headers_remove": [],

            "global_response_headers": {},

            "global_response_headers_remove": [],

            "ignore_endpoint_case": false,

            "global_size_limit": 0,

            "override_target": "",

            "extended_paths": {

                "white_list": [

                    {

                        "path": "/",

                        "ignore_case": false,

                        "method_actions": {

                            "GET": {

                                "action": "reply",

                                "code": 200,

                                "data": "THIS_IS_A_MOCK_RESPONSE_SAMPLE",

                                "headers": {}

                            }

                        }

                    }

                ]

            }

        }

    }

},

"custom_middleware": {

    "auth_check": {

        "name": "RestrictIPLimit(1,60) | SaveRESTHeader(Content-Type,contentTyep) | SaveRESTHeader(Host, host)"

    }

}

}

Hi @Shubham_M_Mishra,

Unfortunately, using a mock response will cause other middleware and authentication to be ignored. You can’t use middleware and mock response in one API definition.

If you want to be able to run the middleware, you’ll have to use one API for the middleware (probably external-facing) API and then another API definition implementing the mock response. The external-facing, middleware-capable API will implement URL rewrite (with looping if you like) to the (probably internal) mock response API.

Hope this makes sense.

Hi @Ubong,

Thanks for the reply!

I am trying to hit the mock response API with tyk://<API_ID>/ but I am getting error msg level=error msg=“http: proxy error: unsupported protocol scheme "tyk"”

After this I tried hitting the mock response API with https://localhost:8080/ and it was successful but the issue is that I have to bypass the ssl verification for this, which is not what I wish to do.

Please help me with this.

Thanks!!

Hi @Shubham_M_Mishra,

Are you past this error? I’m not able to reproduce it.

Please share the API definition.

Hi @Ubong,

Yeah I figured that it was due to the older version 2.5.1 of tyk, when I executed the same on v4.3.4 it worked fine, but but but after hitting API for the middleware (which re_writes to mock response API), the following are my observations:

  1. When the hit is successful, no logs are generated in journalctl
  2. When the hit is not successful, error logs are generated in journalctl.

Please, look into this and let me know! Thanks in advance!