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

request help: edit request body #127

Open
yyl-code opened this issue Feb 12, 2023 · 5 comments
Open

request help: edit request body #127

yyl-code opened this issue Feb 12, 2023 · 5 comments

Comments

@yyl-code
Copy link

Issue description

Does the golang plugin support modifying the request body?

Environment

  • APISIX Go Plugin Runner's version:
  • APISIX version:
  • Go version:
  • OS (cmd: uname -a):
@soulbird
Copy link
Contributor

Not yet supported

@hongdingyi
Copy link

........

@hongdingyi
Copy link

i want change request body

@thinkdb1
Copy link

我试着修改了几个文件,发现可以修改request body值了,先发你先参考看看

apisix-core-with-plugin/internal/http/request.go中
1.加入

func (r *Request) SetBody(b []byte) {
	r.body = b
}

2.在(r *Request) FetchChanges(id uint32, builder *flatbuffers.Builder)中把

if r.path == nil && r.hdr == nil && r.args == nil && r.respHdr == nil {
		return false
	}

改为

if r.path == nil && r.hdr == nil && r.args == nil && r.respHdr == nil && r.body == nil {
		return false
	}

3.在(r *Request) FetchChanges(id uint32, builder *flatbuffers.Builder)中在

if r.path != nil {
		path = builder.CreateByteString(r.path)
	}

后加入

if r.body != nil {
		body = builder.CreateByteString(r.body)
	}

4.在(r *Request) FetchChanges(id uint32, builder *flatbuffers.Builder)中把

var path flatbuffers.UOffsetT

改为

var path, body flatbuffers.UOffsetT

5.在(r *Request) FetchChanges(id uint32, builder *flatbuffers.Builder)中在

if path > 0 {
		hrc.RewriteAddPath(builder, path)
	}

后加入

if body > 0 {
		hrc.RewriteAddBody(builder, body)
	}

6.在apisix-core-with-plugin/pkg/http/http.go中的Request interface加入
SetBody([]byte)
7.在go.mod中github.com/api7/ext-plugin-proto改为v0.6.1

在使用时使用r.SetBody([]byte("mytestbody"))并且在apisix的3.6.0版本下使用就可以了,我只是试用成功了,没测试是否还有其他问题

@TomCN0803
Copy link
Contributor

TomCN0803 commented Nov 18, 2023

I've started a pull request: #151
Features and modifications includes:

  • Update github.com/api7/ext-plugin-proto to v0.6.1
  • Add SetBody([]byte) to http.Request
  • Add a sample request body rewrite plugin
  • Add an e2e test for the sample request body rewrite plugin
  • Lint some code and add items (e.g. .vscode, go.work) to .gitignore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants