Kong / go-pdk

Write Kong plugins in Go! 🦍
https://pkg.go.dev/github.com/Kong/go-pdk
Apache License 2.0
144 stars 48 forks source link

kong.ServiceResponse.GetRawBody() is not working when trying to retrieve response body #165

Closed pongngai closed 4 months ago

pongngai commented 8 months ago

I try to get the response header and body from upstream server by using this code below

func (conf *Config) Response(kong *pdk.PDK) {
    headerKeyResponse, _ := kong.ServiceResponse.GetHeader("x-test")
    log.Println(fmt.Sprintf("HeaderKeyResponse = %v ", headerKeyResponse))

    body, err := kong.ServiceResponse.GetRawBody()
    log.Println(fmt.Sprintf("body = %v err = %v", body, err))
}

The result from logging I can get the header key response but I cannot get the response raw body. This is the error that was printed.

body = err = string field contains invalid UTF-8

I just tried to get the request body by using kong.Request.GetRawBody() as well. It returns an empty byte array but when I try to get the request header everything works fine.

Kong version 3.0.2 Kong config

StarlightIbuki commented 8 months ago

Hi. Could you share the Go PDK version that is used? We changed the API definition in later versions of Kong and Go PDK to return byte arrays to fix this issue. Please consider upgrading to the latest version.

psmolkin commented 6 months ago

Hi, I have the same issue and the same question @StarlightIbuki I don't think we're talking about the same module and function. https://github.com/Kong/go-pdk/blob/master/service/response/response.go#L71 This function returns string and If the body is binary the function returns an error.

Meanwhile, the request.GetRawBody function actually returns byte[]

So I think this is indeed a bug, as go-pdk doesn't provide any way to manipulate the binary body of the response (e.g. gzip) and returns an error

StarlightIbuki commented 5 months ago

@psmolkin @pongngai Sorry for replying late. You're right. This is missed in the fix. We planned the fix in 3.7. Will update when the fix is merged.

StarlightIbuki commented 4 months ago

Internally tracked: KAG-3790