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

Body always empty string #210

Open
refaldyrk opened this issue Jul 5, 2024 · 2 comments
Open

Body always empty string #210

refaldyrk opened this issue Jul 5, 2024 · 2 comments
Labels
question Further information is requested

Comments

@refaldyrk
Copy link

fmt.Println(kong.ServiceResponse.GetRawBody())

I try to call this function but return empty string

how fix this please?

@anthonykzip
Copy link

Were you able to fix this? I'm also seeing the same thing. I also can't seem to print out Service Response headers that I set from the app either.

ex:
In the app handler
w.Header().Set("header", "value")

In Response() of plugin
headers, _ := kong.ServiceResponse.GetHeaders(1000)

headers is empty for some reason.

@gszr
Copy link
Member

gszr commented Jan 14, 2025

Hi @refaldyrk and @anthonykzip,

First of all, thank you for taking the time to report this issue - and our apologies for the delayed response on our end.

Let's dive into this.

  • kong.ServiceResponse.GetRawBody() returns empty string

This requires buffered proxying mode to be on, which in Golang means using the Response phase. Can you elaborate on which phase you used? See the following example:

package main

import (
	"github.com/Kong/go-pdk"
	"github.com/Kong/go-pdk/server"
	"log"
)

func main() {
	server.StartServer(New, Version, Priority)
}

var Version = "0.2"
var Priority = 1

type Config struct {
	Message string
}

func New() interface{} {
	return &Config{}
}


func (conf *Config) Response(kong *pdk.PDK) {
	body, err := kong.ServiceResponse.GetRawBody()
	kong.Log.Err(string(body))
	log.Println(err)
}

This will give you the body.

Alternatively, if you want to use a subsequent phase, you can have an empty Response handler to ensure buffered proxying is enabled.

  • kong.ServiceResponse.GetHeaders(1000) returns no headers

This is a known limitation of the Lua PDK and one we are working on improving; check out Kong/kong#13827 for details. Let's please discuss this particular issue in #222.

@gszr gszr added the question Further information is requested label Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants