Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

response-modifier change http statusCode not effective #932

Closed
Xuqy3 opened this issue Oct 10, 2024 · 0 comments
Closed

response-modifier change http statusCode not effective #932

Xuqy3 opened this issue Oct 10, 2024 · 0 comments
Labels

Comments

@Xuqy3
Copy link

Xuqy3 commented Oct 10, 2024

Environment info:

  • KrakenD version: devopsfaith/krakend:2.7.2
  • System info: docker
  • Backend technology: Java

Describe what are you trying to do:
I need to extract the statusCode field from the response body returned by the backend to replace the http stausCode returned to the user.

The structure of the response body returned by the backend looks like this:

{
    "data": null,
    "statusCode": 400,
    "error": {
        "msg": "custom error message"
    }
}

When the backend api returns a business-customized exception, the http status code is usually 200, and I want the kranken D to return the statusCode in the return body as the http status code to the user.

I'm referring to the sample code provided by https://www.krakend.io/docs/enterprise/extending/plugin-modifiers/ and trying to make a substitution of the status code of the request and it doesn't seem to work. I was able to correctly parse the statusCode field of the returned message by setting output_encording=“json”, but it doesn't seem to be able to be set to an http status code!

type responseWrapper struct {
	ctx        context.Context
	request    interface{}
	data       map[string]interface{}
	isComplete bool
	statusCode int
	headers    map[string][]string
	io         io.Reader
}

func (r registerer) responseDump(
    cfg map[string]interface{},
) func(interface{}) (interface{}, error) {

    // return the modifier
    fmt.Println("response dumper injected!!!")
    return func(input interface{}) (interface{}, error) {
        fmt.Println("response dumper injected!!!")

        resp, _ := input.(proxy.ResponseWrapper)

        modifiedResp := responseWrapper{
            data:       resp.Data(),
            io:         resp.Io(),
            isComplete: resp.IsComplete(),
            headers:    resp.Headers(),
            statusCode: resp.StatusCode(),
        }

        if resp != nil {

            fmt.Println("data:", resp.Data())
            fmt.Println("is complete:", resp.IsComplete())
            //Since output_encording is set to json, the output value here is always map[]
            fmt.Println("headers:", resp.Headers())
            fmt.Println("statusCode:", resp.StatusCode())
			//here I tried to modify its http status code
            modifiedResp.statusCode = 400

            fmt.Println("modified response statusCode:", modifiedResp.statusCode)
        }
        return modifiedResp, nil
    }

Your configuration file:

{
    "$schema": "https://www.krakend.io/schema/v3.json",
    "version": 3,
    "plugin": {
        "pattern": ".so",
        "folder": "/etc/krakend/plugins/"
    },
    "extra_config": {},
    "name": "KrakenD Enterprise API Gateway",
    "port": 8081,
    "host": [
        "http://fake_api"
    ],
    "timeout": "3000ms",
    "cache_ttl": "300s",
    "endpoints": [
        {
            "endpoint": "/users",
            "extra_config": {
                "plugin/req-resp-modifier": {
                    "name": [
                        "krakend-debugger-request",
                        "krakend-debugger-response"
                    ]
                }
            },
            "method": "GET",
            "output_encoding": "json",
            "backend": [
                {
                    "url_pattern": "users",
                    "extra_config": {},
                    "encoding": "json",
                    "sd": "static",
                    "method": "GET",
                    "host": [
                        "http://127.0.0.1:9999"
                    ]
                }
            ],
            "input_query_strings": [
                "*"
            ],
            "input_headers": [
                "*"
            ]
        }
    ]
}

I would very much appreciate advice.

@Xuqy3 Xuqy3 added the question label Oct 10, 2024
@Xuqy3 Xuqy3 closed this as completed Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant