Am I misunderstanding the use_session flag?

Okay. I must be fundamentally misunderstanding something here. I’m running through the examples for Virtual Endpoints listed here. The documentation says “If true then the key session data will be provided to the function as the session variable.” When I set use_session to true, the session object in the javascript file is null, and so calling the endpoint using the sample js code throws “TypeError: Cannot access member ‘meta_data’ of null”. When use_session is false, I can access the session variable in the JS function. Isn’t this backwards?

1 Like

Hey Daisieh,

Thanks for your question.

Are you able to share your .conf file(s) as well as your JS file? This would help me replicate your environment and would help me see what you’re seeing a bit better.


Valmir

js: function myVirtualHandler (request, session, config) {
log(“Virtual Test running”)

log("Request Body: " + request.Body)
log("Session: " + JSON.stringify(session))
log("Config: " + config)
log("param-1: " + request.Params[“param1”]) // case matters
log("auth Header: " + request.Headers[“Authorization”]) // case matters

var responseObject = {
Body: “THIS IS A VIRTUAL RESPONSE”,
Headers: {
“x-test”: “virtual-header”,
“x-test-2”: “virtual-header-2”
},
Code: 200
}

return TykJsResponse(responseObject, session.meta_data)
}
log(“Virtual Test initialised”)

tyk.conf:
{
“secret”: “352d20ee67be67f6340b4c0605b044b7”,
“template_path”: “/opt/tyk-gateway/templates”,
“tyk_js_path”: “/opt/tyk-gateway/js/tyk.js”,
“middleware_path”: “/opt/tyk-gateway/middleware”,
“use_db_app_configs”: false,
“app_path”: “/opt/tyk-gateway/apps/”,
“storage”: {
“type”: “redis”,
“username”: “”,
“password”: “”,
“database”: 0,
“optimisation_max_idle”: 2000,
“optimisation_max_active”: 4000
},
“enable_analytics”: false,
“analytics_config”: {
“type”: “csv”,
“csv_dir”: “/tmp”,
“mongo_url”: “”,
“mongo_db_name”: “”,
“mongo_collection”: “”,
“purge_delay”: -1,
“ignored_ips”: []
},
“health_check”: {
“enable_health_checks”: true,
“health_check_value_timeouts”: 60
},
“optimisations_use_async_session_write”: true,
“enable_non_transactional_rate_limiter”: true,
“enable_sentinel_rate_limiter”: false,
“enable_redis_rolling_limiter”: false,
“allow_master_keys”: false,
“policies”: {
“policy_source”: “file”,
“policy_record_name”: “/opt/tyk-gateway/policies/policies.json”
},
“hash_keys”: true,
“close_connections”: false,
“http_server_options”: {
“enable_websockets”: true
},
“allow_insecure_configs”: true,
“coprocess_options”: {
“enable_coprocess”: true,
“coprocess_grpc_server”: “”
},
“enable_bundle_downloader”: true,
“bundle_base_url”: “”,
“global_session_lifetime”: 100,
“force_global_session_lifetime”: false,
“max_idle_connections_per_host”: 500,
“enable_jsvm”: true,
“hash_key_function”: “murmur64”
}

api.json:
{
“name”: “Test API”,
“slug”: “test-api”,
“api_id”: “1”,
“org_id”: “1”,
“use_keyless”: true,
“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,
“extended_paths”: {
“ignored”: [],
“black_list”: [],
“white_list”: [],
“virtual”: [
{
“response_function_name”: “myVirtualHandler”,
“function_source_type”: “file”,
“function_source_uri”: “middleware/testVirtual.js”,
“path”: “/2anythingvirt”,
“method”: “GET”,
“use_session”: true
}
]
}
}
}
},
“proxy”: {
“listen_path”: “/test-api/”,
“target_url”: “http://httpbin.org/”,
“strip_listen_path”: false
},
“active”: true
}

Any progress on this?

Hello @daisieh,

I was actually able to replicate the exact behaviour you described. Give me sometime as I am looking for answers for this as well.

Zaid

Hello @daisieh,

I have submitted a bug request for this.

Zaid

2 Likes

Hi Zaid,

Could you please post the link to the bug request so it helps the documentation chain for those who want to know?

Thank you

@Amanjeev_Sethi I am sorry but that is on our internal JIRA I’ll check if there is a way to expose those but I don’t think so.

any updates? best :slightly_smiling_face:

The ticket is currently being looked at I believe that the fix should be out in the next couple of versions but don’t hold me to that. The reason this is taking longer is because this is a lower priority ticket unfortunately.