Modify request body in UDG

Hi guys so I’m using UDG here and I want to modify the client’s query (located in request body) in pre Go plugin. Can we do that there? Maybe , you can show me an example?

When I tried to modify the body request in the pre-request plugin , the Tyk returned an error ‘Error while unmarshalling GraphQL: the provided request is empty’.

Not tried this. Would try and let you know how it goes.

Hi @midnight,

I wonder if you’ve resolved this.

Can you share your plugin code, so we take a look ?

Hi, i have resolved this issue. I think previously I made some mistake when marshalling the query string :D. When i tried it again it works

const modifiedReq = `{"query":"query testMultipleEndpoint {  getUser(id: 0) {    user {      name      id      status      pokebag{        pokemons{          name        }        error {          code          message          path        }            }    }    error{      code      message      path    }  }  getPokemon(userid:0){    pokemons{      name      user_id    }    error{      code      message      path    }  }  }","variables":null,"operationName":"testMultipleEndpoint"}`

func ModifyReqQueryX(rw http.ResponseWriter, r *http.Request) {
	logger.Info("=== ModifyReqQueryX ===")
	//req is the query request
	b, err := io.ReadAll(r.Body)
	if err != nil {
	logger.Info("Request:  ", string(b))

	// overwrite our req body and update the content length
	newReqByte := []byte(modifiedReq)
	var buf bytes.Buffer
	r.Body = ioutil.NopCloser(&buf)
	r.Header.Set("Content-Length", fmt.Sprint(len(newReqByte)))


1 Like

Why am I getting the following error even after setting the Content-Length header to the len of the new request?

http: proxy error: http: ContentLength=2607 with Body length 2636

I think there is something wrong when you process the length of the new request. Because the content length and body length should be the same

1 Like

using r.ContentLength = int64((len(newReqByte))) was the hack

1 Like