Reject request if header is missing

Using the tyk pro trial to evaluate some things

If I have a inbound API request going through the gateway, how can I send back a specific HTTP status code if a particular custom HTTP header is NOT present in the request?

i.e.

if HTTP header “x-my-header” is not present, respond w/ a 500

Hi Bitsofinfo,

you’d need to write a custom JS middleware.
For more information see this section in our docs, note the ReturnOverrides.

Thanks,
Kos @ Tyk Support Team

Have been trying this using a “virtualpath” plugin, can I do it that way w/ a function?

Hi Bitsofinfo,

It is possible but it’s more tricky because VP are more oriented to specific path/method combination so you’d have to extend the API spec for each case.
Writing a middleware will apply to all cases.

Thanks,
Kos @ Tyk Support Team

Kos - ok, well I’m trying a simple function on one path but it doesnt seem to be invoked, any way to debug this?

I’m using the docker pro trial sample compose launched setup

I also read that it is invoked AFTER the route is processed? Could this be why? Says: “you can plug short JavaScript functions at the end of a Tyk route and have them run when the endpoint is called.”

How do I “install” a middlware JS file to invoke in this docker setup?

Hi Bitsofinfo, can you share your container log? It would be useful to see if the required components are being loaded.

I’m just using the sample docker compose based setup, which container logs should I be looking at (gateway?)

sed: cannot rename /opt/tyk-gateway/sedDsvDVP: Device or resource busy
sed: cannot rename /opt/tyk-gateway/sedVnRQVO: Device or resource busy
time="Jul 12 18:07:10" level=info msg="Connection dropped, connecting.."
time="Jul 12 18:07:10" level=info msg="Starting Poller"
time="Jul 12 18:07:10" level=info msg="PIDFile location set to: /var/run/tyk-gateway.pid"
time="Jul 12 18:07:10" level=error msg="Instrumentation is enabled, but no connectionstring set for statsd"
time="Jul 12 18:07:10" level=info msg="Initialising Tyk REST API Endpoints"
time="Jul 12 18:07:10" level=info msg="--> Standard listener (http)"
time="Jul 12 18:07:10" level=info msg="Setting up Server"
time="Jul 12 18:07:10" level=info msg="Registering node."
time="Jul 12 18:07:10" level=error msg="Request failed: Get http://tyk_dashboard:3000/register/node: dial tcp 172.23.0.4:3000: getsockopt: connection refused"
time="Jul 12 18:07:12" level=warning msg="Insecure configuration detected (allowing)!"
time="Jul 12 18:07:15" level=info msg="Node registered" id=2aff23df-45f3-4cce-512c-e412402e41b7
time="Jul 12 18:07:15" level=info msg="Initialising distributed rate limiter"
time="Jul 12 18:07:15" level=info msg="Starting gateway rate imiter notifications..."
time="Jul 12 18:07:15" level=info msg="Using /opt/tyk-gateway/tyk.conf for configuration"
time="Jul 12 18:07:15" level=info msg="Detected 0 APIs"
time="Jul 12 18:07:15" level=info msg="Loading API configurations."
time="Jul 12 18:07:16" level=info msg="Loading uptime tests..."
time="Jul 12 18:07:16" level=info msg="Initialised API Definitions"
time="Jul 12 18:07:16" level=info msg="Loading policies"
time="Jul 12 18:07:16" level=info msg="Using Policies from Dashboard Service"
time="Jul 12 18:07:16" level=info msg="Mutex lock acquired... calling"
time="Jul 12 18:07:16" level=info msg="Calling dashboard service for policy list"
time="Jul 12 18:07:16" level=info msg="Processing policy list"
time="Jul 12 18:07:16" level=info msg="Gateway started (v2.3.6)"
time="Jul 12 18:07:16" level=info msg="--> Listening on address: (open interface)"
time="Jul 12 18:07:16" level=info msg="--> Listening on port: 8080"
time="Jul 12 18:07:16" level=info msg="--> PID: 9"

Hi Bitsofinfo,

can you share your tyk.conf ?

Thanks,
Kos @ Tyk Support Team

{
    "listen_port": 8080,
    "secret": "352d20ee67be67f6340b4c0605b044b7",
    "node_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": true,
    "db_app_conf_options": {
        "connection_string": "http://tyk_dashboard:3000",
        "node_is_segmented": false,
        "tags": ["test2"]
    },
    "app_path": "/opt/tyk-gateway/apps/",
    "storage": {
        "type": "redis",
        "host": "redis",
        "port": 6379,
        "username": "",
        "password": "",
        "database": 0,
        "optimisation_max_idle": 100
    },
    "enable_analytics": true,
   "analytics_config": {
        "type": "mongo",
        "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,
    "allow_master_keys": false,
    "policies": {
        "policy_source": "service",
        "policy_connection_string": "http://tyk_dashboard:3000",
        "policy_record_name": "tyk_policies"
    },
   "hash_keys": true,
    "close_connections": true,
    "allow_insecure_configs": true,
     "coprocess_options": {
        "enable_coprocess": false,
        "coprocess_grpc_server": ""
    },
    "enable_bundle_downloader": true,
    "bundle_base_url": "",
    "global_session_lifetime": 100,
    "force_global_session_lifetime": false,
    "max_idle_connections_per_host": 100
}

Hi @bitsofinfo, you should add "enable_jsvm": true to your tyk.conf, see here.

Enabling that fixed it, thanks!