Response modifier plugin doesn't work when request fails

36 views
Skip to first unread message

Fuat Bozkurt

unread,
Apr 8, 2024, 5:20:56 AMApr 8
to KrakenD Community
Hi guys,

I'm using krakend community. I have a response modifier plugin which converts response from camelCase to PascalCase. The plugin was built in Golang.
I realize that when backend failed (status 401, 403 etc.) plugin doesn't work. To test the plugin I put some logs, when backend was OK I could see the logs but when backend fails I couldn't see logs.

To give some info also added these configs to krakend;
"backend/http": {
   "return_error_code": true
}  

"router": {
  return_error_msg": true
}

Do you have any suggestion this issue?

Thanks,

Albert Lombarte

unread,
Apr 12, 2024, 3:30:41 AMApr 12
to KrakenD Community, fuatbo...@gmail.com
Hi Fuat,

The rest of the modification path is aborted as soon as an error occurs. Plugins do not work with failed data on a pipe meant for manipulation. If think that if you use no-op in your output_encoding, then you can do whatever you want with the data, because KrakenD does not parse the response.

If your endpoint uses an output_encoding other than no-op you can work with ResponseWrapper.Data() and ResponseWrapper.IsComplete(). If you use no-op you can work with ResponseWrapper.Io(), ResponseWrapper.StatusCode(), and ResponseWrapper.Headers().

Hope this helps
El dia dilluns, 8 d’abril del 2024 a les 11:20:56 UTC+2, fuatbo...@gmail.com va escriure:

Fuat Bozkurt

unread,
Apr 12, 2024, 1:20:23 PMApr 12
to KrakenD Community, Albert Lombarte, fuatbo...@gmail.com
Hi Albert,

Firstly thanks for your answer. My endpoint uses "json" for output_encoding and encoding. 
To clarify the issue Please correct me if I misunderstood, If I want to modify response when it is failed I should use no-op for output_encoding?

If I have to use no-op encoding, still I'll be able to modify my response as json?

Thanks in advance, 

12 Nisan 2024 Cuma tarihinde saat 10:30:41 UTC+3 itibarıyla Albert Lombarte şunları yazdı:

Fuat Bozkurt

unread,
Apr 16, 2024, 4:46:38 AMApr 16
to KrakenD Community, Fuat Bozkurt, Albert Lombarte
I tried output_encoding as "no-op" now plugin is working (i can see the logs on container). But now i cannot see the response body.
Backend returns StatusCode = 403 or 401 with some json data in body (checked with postman). In plugin it reads data with "ResponseWrapper.Io()" and makes some manipulation and returns it. 
I could see response data just before returning it ( the property "m" in the code below) but in the application the response body is empty (in the response header content-length is 0) 
 
Screenshot 2024-04-16 114423.png

Plugin code
       
resp, ok := input.(ResponseWrapper) // read response
res, err = io.ReadAll(resp.Io()) // read body from ResponseWrapper.Io() as byte[]
encoded, _ := json.MarshalIndent(conventionalMarshaller{res}, "", "  ") // some manipulation with json data

        m := make(map[string]interface{})
        json.Unmarshal(encoded, &m) // now "m" is modified response data. When printed m, I see the modified data.
       
        respWrapper := responseWrapper{
            data: m, // modified data
            isComplete: resp.IsComplete(),
            metadata: metadataWrapper{
                headers: resp.Headers(),
                statusCode: resp.StatusCode(),
            },
        }

        return respWrapper, nil




12 Nisan 2024 Cuma tarihinde saat 20:20:23 UTC+3 itibarıyla Fuat Bozkurt şunları yazdı:

Fuat Bozkurt

unread,
Apr 25, 2024, 7:29:20 AMApr 25
to KrakenD Community, Fuat Bozkurt, Albert Lombarte

Do you have any suggestions ?
16 Nisan 2024 Salı tarihinde saat 11:46:38 UTC+3 itibarıyla Fuat Bozkurt şunları yazdı:
Reply all
Reply to author
Forward
0 new messages