API definitions on virtualendpoint JSVM function

Hi All,

I am using tyk_quickstart module with gateway version 2.3.7, i have tried to create a virtual endpoint. log is observed in the gateway console logs, once the api is being triggered it is throwing 404 and the console logs
[time=“Jan 23 02:45:04” level=error msg=“Failed to decode virtual endpoint response data on return from VM: invalid character ‘u’ looking for beginning of value”
time=“Jan 23 02:45:04” level=error msg=“–> Returned: undefined” ]

Does any one have idea about this.

API URL : localhost/test/
Endpoint for virtual path is /{testscript}
Tested using this path at postman : http://localhost:8080/test/testscript

tyk.conf

“coprocess_options”: {
“enable_coprocess”: true,
“coprocess_grpc_server”: “”
},
“enable_jsvm”:true

Any sugestions, where it wrong?

Appreciate for your patience.

Hi
Can you please send your api definition?

{
“id”: “5a6699f23b7fa80001154a50”,
“name”: “test”,
“slug”: “test”,
“api_id”: “b55bccf367be44e468604a6bd60a9e6a”,
“org_id”: “5a6699233b7fa80001154a4a”,
“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,
“versions”: {
“Default”: {
“name”: “Default”,
“expires”: “”,
“paths”: {
“ignored”: [],
“white_list”: [],
“black_list”: []
},
“use_extended_paths”: true,
“extended_paths”: {
“virtual”: [
{
“response_function_name”: “testscript”,
“function_source_type”: “blob”,
“function_source_uri”: “ZnVuY3Rpb24gc29tZXRlc3QocmVxdWVzdCwgc2Vzc2lvbiwgY29uZmlnKXsKICAgIGNvbnNvbGUubG9nKCJJIHdhcyB0ZXN0aW5nIHRoaXMgZnVuY3Rpb24gaG93IGl0IHRyaWdnZXJzIik7CiAgICB2YXIgcmVzcG9uc2VPYmplY3QgPSB7CiAgICAgICBCb2R5OiAiVEhJUyBJUyBBICBWSVJUVUFMIFJFU1BPTlNFIiwKICAgICAgSGVhZGVyczogewogICAgICAgICJ0ZXN0IjogInZpcnR1YWwiLAogICAgICAgICJ0ZXN0LTIiOiAidmlydHVhbCIKICAgIH0sCiAgICBDb2RlOiAyMDAKICAgIH0KICAgIHJldHVybiBUeWtKc1Jlc3BvbnNlKHJlc3BvbnNlT2JqZWN0LCBzZXNzaW9uLm1ldGFfZGF0YSkKfQpsb2coImF5eWFwcGEgc2NyaXB0IGlzIGNhbGxpbmcgYW5kIHByaW50aW5nIik=”,
“path”: “/{testscript}”,
“method”: “GET”,
“use_session”: false
}
]
},
“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”: “/test/”,
“target_url”: “http://httpbin.org/”,
“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
},
“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”: [],
“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
}

As i have used quickstart with dockers, so i was trying to use GUI dashboard to configure my APIs.

I encountered some different issues to you.

I recommend making the following changes:

  1. All quote characters in your API definition to be " rather than the start and end quote characters.
  2. Update the API endpoint path to be testscript, you don’t need the proceeding forward slash or curly brackets. Curly brackets are used for wildcards.
  3. Update the JS function to call to be sometest since that is the name of the function in your virtual endpoint JS code.

Once I made those amendments the virtual response code ran successfully.

Hi David,

if you don’t mind can you post the API definition, i got understand 2,3 points, but not the first one.
so please post the updated API definition.

Thanks

I got this below response.

time=“Jan 23 04:34:22” level=error msg=“Failed to decode virtual endpoint response data on return from VM: invalid character ‘u’ looking for beginning of value”
time=“Jan 23 04:34:22” level=error msg="–> Returned: undefined"

Here’s my updated definition. Note that I updated the name and slug to test2 to avoid a conflict with an existing API definition on my system.

{
    "id": "5a66b46d56c02c023fbc378c",
    "name": "test2",
    "slug": "test2",
    "api_id": "efeedb9bd93a46c85abfe678987d35bb",
    "org_id": "59bfdf5b56c02c065d24638e",
    "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,
        "versions": {
            "Default": {
                "name": "Default",
                "expires": "",
                "paths": {
                    "ignored": [],
                    "white_list": [],
                    "black_list": []
                },
                "use_extended_paths": true,
                "extended_paths": {
                    "virtual": [
                        {
                            "response_function_name": "sometest",
                            "function_source_type": "blob",
                            "function_source_uri": "ZnVuY3Rpb24gc29tZXRlc3QocmVxdWVzdCwgc2Vzc2lvbiwgY29uZmlnKXsKICAgIGNvbnNvbGUubG9nKCJJIHdhcyB0ZXN0aW5nIHRoaXMgZnVuY3Rpb24gaG93IGl0IHRyaWdnZXJzIik7CiAgICB2YXIgcmVzcG9uc2VPYmplY3QgPSB7CiAgICAgICBCb2R5OiAiVEhJUyBJUyBBICBWSVJUVUFMIFJFU1BPTlNFIiwKICAgICAgSGVhZGVyczogewogICAgICAgICJ0ZXN0IjogInZpcnR1YWwiLAogICAgICAgICJ0ZXN0LTIiOiAidmlydHVhbCIKICAgIH0sCiAgICBDb2RlOiAyMDAKICAgIH0KICAgIHJldHVybiBUeWtKc1Jlc3BvbnNlKHJlc3BvbnNlT2JqZWN0LCBzZXNzaW9uLm1ldGFfZGF0YSkKfQpsb2coImF5eWFwcGEgc2NyaXB0IGlzIGNhbGxpbmcgYW5kIHByaW50aW5nIik=",
                            "path": "testscript",
                            "method": "GET",
                            "use_session": false
                        }
                    ]
                },
                "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": "/test2/",
        "target_url": "http://httpbin.org/",
        "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
    },
    "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": [],
    "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
}

Excellent. It did worked. Thanks david, you made my day…

3 Likes