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

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

Closed
pongngai opened this issue Nov 8, 2023 · 4 comments · Fixed by #195
Closed
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@pongngai
Copy link

pongngai commented Nov 8, 2023

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
- KONG_DATABASE=off
- KONG_DECLARATIVE_CONFIG=/usr/local/kong/declarative/kong.yml
- KONG_PLUGINS=bundled,go-plugin
- KONG_PLUGINSERVER_NAMES=go-plugin
- KONG_PLUGINSERVER_GO_PLUGIN_START_CMD=/usr/local/bin/go-plugin
- KONG_PLUGINSERVER_GO_PLUGIN_QUERY_CMD=/usr/local/bin/go-plugin -dump

@StarlightIbuki
Copy link
Contributor

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
Copy link

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
Copy link
Contributor

@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.

@gszr gszr added bug Something isn't working and removed pending author feedback labels Feb 22, 2024
@StarlightIbuki
Copy link
Contributor

StarlightIbuki commented Feb 22, 2024

Internally tracked: KAG-3790

@gszr gszr added the good first issue Good for newcomers label Feb 22, 2024
gszr added a commit that referenced this issue Mar 13, 2024
Correctly return a byte array instead of string.

:warning: BREAKING CHANGE:
- This is a needed breaking change to fix the behavior of the method.
  Your plugins must be updated to correctly handle the new return value
  type of `[]byte` rather than `string`.

Fixes #165.
@gszr gszr closed this as completed in #195 Mar 13, 2024
gszr added a commit that referenced this issue Mar 13, 2024
Correctly return a byte array instead of string.

:warning: BREAKING CHANGE:
- This is a needed breaking change to fix the behavior of the method.
  Your plugins must be updated to correctly handle the new return value
  type of `[]byte` rather than `string`.

Fixes #165.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants