Go Plugin | Response Handler | function sent error response code

Hi, I’ve developed a Go plugin that transforms incoming requests before they reach the upstream server. This transformation involves querying a third-party service, which may occasionally respond with error status codes (400, 404, 422, 500, etc.). When an error occurs, the Go plugin returns the third party service’s response (including the body and status code) directly to the client, bypassing the upstream server. However, the Tyk API Gateway modifies my response with its own response handler, as it seems to interpret the plugin’s execution as an error.

This is the response body received by the client:

{
    "code": "INVALID_INPUT",
    "message": "Invalid request parameters",
    "details": [
        {
            "field": "code",
            "message": "Field required"
        }
    ]
}{
    "error": "plugin function sent error response code: 400"
}

The message “error”: “plugin function sent error response code: 400” has been added by Tyk itself, because of its built-in Response Handler: tyk/gateway/res_handler_go_plugin.go at 64c2a7fa7bcebe71e32f8465b02e43f078ff6ddd · TykTechnologies/tyk · GitHub.

I don’t want to return this information to the client; is there a configuration setting in Tyk to disable the default response handler? As custom plugins are described as supporting virtual endpoints in the documentation, I’m expecting them to function without interference from the default response handler.

This is my code:

        if statusCode >= 400 {
                fmt.Printf("Response from %s: status code %d\n", postURL, statusCode)

                responseJSON, err := json.Marshal(newResponse)
                if err != nil {
                        fmt.Println("Error in converting response to JSON:", err)
                        http.Error(rw, "Internal Server Error", http.StatusInternalServerError)
                        return
                }

                rw.Header().Set("Content-Type", "application/json")
                rw.WriteHeader(statusCode)
                rw.Write(responseJSON)
                return
        }

Thank you for the support!

1 Like

Hi @Davide_Ighina,

Welcome to the community :partying_face:

This looks like a known bug which is fixed in 5.3LTS and from v5.0.7 and v5.2.2.
Can you check this, or confirm your Gateway version?

1 Like

Hi @Ubong, thank you!
My gateway version is: v5.7.2

1 Like

Hi @Davide_Ighina,

This might be the case if you’re using a Golang Virtual endpoint plugin.
This issue has been identified and is being tracked with reference TT-13436
Please see issue on Github

You shouldn’t experience this if you use a pre-request plugin as you describe. Can you confirm?