Response modification on KrakenD req-resp-modifier plugin

162 views
Skip to first unread message

Evghenii Garbuzneac

unread,
Mar 21, 2024, 10:20:10 AM3/21/24
to KrakenD Community
Hi all.

I've written a plugin corresponding to that article. https://www.krakend.io/docs/extending/plugin-modifiers/ 

My plugin converts XML response from API to JSON.

But now I want to add one improvement: I need to convert XML to JSON only if Accept: application/json is set in the request. 

So, the question is: how I can get access to request headers inside responseModifier function? 

Some parts of my code:

func (r registerer) RegisterModifiers(f func(
name string,
factoryFunc func(map[string]interface{}) func(interface{}) (interface{}, error),
appliesToRequest bool,
appliesToResponse bool,
)) {
f(string(r)+"-request", r.requestModifier, true, false)
f(string(r)+"-response", r.responseModifier, false, true)
fmt.Println(string(r), "registered!!!")
}

-------------

func (r registerer) requestModifier(
cfg map[string]interface{},
) func(interface{}) (interface{}, error) {
    return func(input interface{}) (interface{}, error) {
        return input, nil
    }
}


func (r registerer) responseModifier(
cfg map[string]interface{},
) func(interface{}) (interface{}, error) {
        return func(input interface{}) (interface{}, error) {
       // need logic to check request headers
       if !hasAcceptHeder {
           return input, nil
       }
       modifiedResp := ......
       return modifiedResp, nil
    }
}

Albert Lombarte

unread,
Mar 21, 2024, 11:11:23 AM3/21/24
to KrakenD Community, evghe...@optimum-web.com
Hi Evghenii,

You don't need any plugin to get this behavior, as KrakenD does it automatically. If you choose an output_encoding = negotiate then KrakenD will honor the conversion.



El dia dijous, 21 de març del 2024 a les 15:20:10 UTC+1, evghe...@optimum-web.com va escriure:

Evghenii Garbuzneac

unread,
Mar 21, 2024, 1:04:24 PM3/21/24
to KrakenD Community, Albert Lombarte, evghe...@optimum-web.com
Hi Albert, 
Thank you for your response.
Yes, I know about https://www.krakend.io/docs/endpoints/content-types/#working-with-non-json 
But we also change the structure of the response for JSON, for example, we need to remove some nodes

четверг, 21 марта 2024 г. в 17:11:23 UTC+2, Albert Lombarte:

Albert Lombarte

unread,
Mar 21, 2024, 1:11:37 PM3/21/24
to KrakenD Community, evghe...@optimum-web.com, Albert Lombarte

El dia dijous, 21 de març del 2024 a les 18:04:24 UTC+1, evghe...@optimum-web.com va escriure:

Evghenii Garbuzneac

unread,
Mar 21, 2024, 1:16:08 PM3/21/24
to KrakenD Community, Albert Lombarte, evghe...@optimum-web.com
Yes, it will work if we want to remove any child nodes.
But it will not work if we want to change the structure:
Node1
      Node1.1
          Node1.1.1
          Node1.1.2
     Node1.2
          Node1.2.1

To:
{
      Node1.1.1: "",
      Node1.2.1: ""
}

I didn't find the way to do that in the CE version, on in the EE
     

четверг, 21 марта 2024 г. в 19:11:37 UTC+2, Albert Lombarte:

Albert Lombarte

unread,
Mar 21, 2024, 1:17:30 PM3/21/24
to Evghenii Garbuzneac, KrakenD Community
Try https://www.krakend.io/docs/backends/flatmap/ before over complicating your installation
Logo
Albert Lombarte
CEO

LinkedIn icon   Twitter icon  


Missatge de Evghenii Garbuzneac <evghe...@optimum-web.com> del dia dj., 21 de març 2024 a les 18:16:

Evghenii Garbuzneac

unread,
Mar 21, 2024, 3:11:37 PM3/21/24
to KrakenD Community, Albert Lombarte, KrakenD Community, Evghenii Garbuzneac
Hi Albert. 

четверг, 21 марта 2024 г. в 19:17:30 UTC+2, Albert Lombarte:
Reply all
Reply to author
Forward
0 new messages