How to add consumer group name to response headers

Greetings :wave:

In Kong gateway I’m trying to return consumer group name in response headers.

What I tried to do:

I added a “Key Authentication” plugin to identify consumers, and a “Kong Functions (Post-Plugins)” to get the consumer’s group and add it to the response headers.

Assuming each consumer belongs to one consumer group in my setup, this is the content of my Kong function plugin in the access phase:

local consumer = kong.client.get_consumer()
local consumer_groups = kong.client.get_consumer_groups(consumer.id)
kong.response.set_header(“consumer_group”, consumer_groups[1])

This does not add the header to the response. I’m assuming it’s because consumer_groups[1] is nil because the following returns the consumer id in the response headers:

local consumer = kong.client.get_consumer()
kong.response.set_header(“consumer_id”, consumer.id)

I need help understanding why consumer group was not returned in response headers, or if there is another way of adding it to response headers.

Thanks.

Found it, I can get group name this way:
consumer_groups[1][“name”]

I thought consumer_groups[1] would return the name based on this:

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.