apache / apisix-go-plugin-runner

Go Plugin Runner for APISIX
https://apisix.apache.org/
Apache License 2.0
167 stars 69 forks source link

request help: edit request body #127

Open yyl-code opened 1 year ago

yyl-code commented 1 year ago

Issue description

Does the golang plugin support modifying the request body?

Environment

soulbird commented 1 year ago

Not yet supported

hongdingyi commented 1 year ago

........

hongdingyi commented 1 year ago

i want change request body

thinkdb1 commented 8 months ago

我试着修改了几个文件,发现可以修改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 commented 7 months ago

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

Eslam3bdel3al commented 3 days ago

the setBody([]byte) works fine when using only "ext-plugin-pre-req" once using both "ext-plugin-pre-req" and "ext-plugin-post-resp", it doesn't work.