How to transform query parameters from camelCase to snake_case?

Hi, how to transform query parameters from camelCase to snake_case?

For example,
https://gateway.com?firstName=John&lastName=Doe
to
https://upstream.com?first_name=John&last_name=Doe

Hi @JJ_Teoh and welcome to the community.

I think you could achieve your goal with a custom plugin. We don’t have an inbuilt middleware that allows you to transform the query parameters.

Your plugin would be in the Request lifecycle and would take advantage of the request object. We have a few examples that can get you started on using custom plugins

Hey @Olu, thank for giving me the direction. I am trying out building my own custom plugin, I am having trouble when the query parameters are array, for example ?orders=A001&orders=A002.

It seem like the request.AddParams expecting string instead of array. So, when I tried to do this:

request.AddParams["orders"] = ["A001", "A002"]

I am getting this error:

 cannot unmarshal array into Go struct field MiniRequestObject.Request.AddParams of type string

What language are you using?

I think the value should be a string instead of an array. That’s how I think query params should be

request.AddParams["key"] = "value"

I am using Javascript(JSVM).

We do have usecases that accept array in query params in the following format:

https://example.com/users?firtName=John&firstName=Joe

This is legit query params as described in here under Query Parameters section.

From what I observe from the logs, the initial request.Params allow array, but not working for request.AddParams.

log(request.Params) shows

{"firstName":["John","Joe"]}

Thanks for confirming your custom plugin language.

An array does not work for AddParams because the schema used to describe it is a map[string]string as shared in the request object.

The schema for Params as you outlined shows it is a map[string][]string. I am not sure if modifying this value works. But it may be worth a try.