Not able to execute the virtual path

Hi,

I was trying to create a new API in tyk. I installed the docker version of tyk in my local.

The proxy is working fine along with the token.

Now when I’m trying to set up a virtual path for that API from the dash board (in the endpoint designer section/ tab), it is not working.

So, it is not invoking the js function i added, it is only calling the target url and giving the response.

Can you please help here. I also attached the json export for the api configured.

{
“id”: “59a407ca5bc4890001e167ed”,
“name”: “CustomerByContact”,
“slug”: “customerbycontact”,
“api_id”: “780e510654fd479f59b7f30d6c2d3835”,
“org_id”: “59a404d65bc4890001e167e7”,
“use_keyless”: false,
“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”: “Authorization”
},
“use_basic_auth”: false,
“enable_jwt”: false,
“use_standard_auth”: true,
“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”: “myVirtualHandler”,
“function_source_type”: “blob”,
“function_source_uri”: “ZnVuY3Rpb24gbXlWaXJ0dWFsSGFuZGxlciAocmVxdWVzdCwgc2Vzc2lvbiwgY29uZmlnKSB7CiAgICAgICAgbG9nKCJWaXJ0dWFsIFRlc3QgcnVubmluZyIpCiAgICAKICAgICAgICBsb2coIlJlcXVlc3QgQm9keTogIikKICAgICAgICBsb2cocmVxdWVzdC5Cb2R5KQogICAgCiAgICAgICAgbG9nKCJTZXNzaW9uOiAiKQogICAgICAgIGxvZyhzZXNzaW9uKQogICAgCiAgICAgICAgbG9nKCJDb25maWc6IikKICAgICAgICBsb2coY29uZmlnKQogICAgCiAgICAgICAgbG9nKCJwYXJhbS0xOiIpCiAgICAgICAgbG9nKHJlcXVlc3QuUGFyYW1zWyJwYXJhbTEiXSkKICAgIAogICAgICAgIHZhciByZXNwb25zZU9iamVjdCA9IHsKICAgICAgICAgICAgQm9keTogIlRISVMgSVMgQSAgVklSVFVBTCBSRVNQT05TRSIKICAgICAgICAgICAgSGVhZGVyczogewogICAgICAgICAgICAgICAgInRlc3QiOiAidmlydHVhbCIsCiAgICAgICAgICAgICAgICAidGVzdC0yIjogInZpcnR1YWwiCiAgICAgICAgICAgIH0sCiAgICAgICAgICAgIENvZGU6IDIwMAogICAgICAgIH0KICAgIAogICAgICAgIHJldHVybiBUeWtKc1Jlc3BvbnNlKHJlc3BvbnNlT2JqZWN0LCBzZXNzaW9uLm1ldGFfZGF0YSkgICAKICAgIH0KICAgIGxvZygiVmlydHVhbCBUZXN0IGluaXRpYWxpc2VkIik=”,
“path”: “http://www.tyk-portal-test.com/customer/contact/{id}”,
“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”: “/customer/contact/{id}”,
“target_url”: “http://10.32.0.8:8073/”,
“strip_listen_path”: false,
“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”: “hostname”,
“port_data_path”: “port”,
“target_path”: “/api-slug”,
“use_target_list”: false,
“cache_timeout”: 60,
“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”: null
},
“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”: {}
}

Hi, is JSVM enabled in your Tyk main configuration file?

Would be useful to see your gateway log as well.

Regards.

Hi,

Thanks for the quick response.

I looked into both tyk.conf and tyk_analysis.conf for such option, but not able to find any.

Also in the gateway log its empty … seems the virtual endpoint is not triggered at all.

Can you please let me know the place where i need to change to enable the jsvm.

Thanks,
Abhishek

Hi Abhishek, you can take a look here:

https://tyk.io/docs/customise-tyk/plugins/javascript-middleware/middleware-scripting-guide/

Add enable_jsvm: "true" to your tyk.conf

Best

Hi,

Thanks, I can execute the virtual endpoint now.

I tried to execute the javascript given in the link : https://tyk.io/docs/tyk-dashboard-v0-9/plugins/virtual-endpoints/ … and it is giving me the response as “THIS IS A VIRTUAL RESPONSE”.

But when I’m trying to execute the following js I’m not able to understand if it is running properly or not. Also the logs are not generated in the gateway logs section.

newRequest = {
“Method”: “GET”,
“Headers”: {},
“Domain”: “http://10.32.0.8:8073
//“Resource”: “/customer/contact/”
};

function myVirtualHandler (request, session, config) {
console.log(“Virtual Test running”)

    log("Request Body: ")
    log(request.Body)

    log("Session: ")
    log(session)

    log("Config:")
    log(config)

    rawlog(config)

    log("param-1:")
    log(request.Params["param1"])

    var urlPath = request.url
    newRequest.Resource = urlPath

    var responseObject =  TykMakeHttpRequest(JSON.stringify(newRequest))
    log("responseObject:")
    log(responseObject)

    return TykJsResponse(responseObject, session.meta_data)   
} 

Also I’ve some questions in general,

  1. I have used body transformation of response and its working. But I want to transform the response by based on some profile coming in the request header. So can I route to the correct body transformer based of the profile value. I dont want to introduce the version. So is it doable.

  2. In case of caching can i push the response manually in the cache store and pull it from the cache. And i want to perform these operations from virtual path javascript function. Is it possible.

You need to do this with a virtual endpoint, the Response body transform doesn’t have access to header information. The request one does.

The virtual path response can be cached if holy have caching enabled, but you don’t have access to the cache from the JS function.

To debug your code, add log() calls at each step, when you load the gateway the logs will appear there to see whether it’s a syntax error.

99% of the time it’s a syntax error - try editing the code Ina JS IDE to make sure you’re initialising everything properly.