1

I have been playing with Littleproxy and found it easy to modify responses, but can't get any good example of modifying request parameters.

On the net I have found only one example, modifying post data. But cannot make it work.

Would you have an example explaining how we can get the request parameters and modify them.

I have this:

public HttpResponse proxyToServerRequest(HttpObject httpObject) {

    if(httpObject instanceof FullHttpRequest){
        FullHttpRequest request = (FullHttpRequest) httpObject;

        if(request.getMethod() == HttpMethod.POST
                && request.getUri().contains("/post")){

            CompositeByteBuf contentBuf = (CompositeByteBuf) request.content();           

            String contentStr = contentBuf.toString(CharsetUtil.UTF_8);

            System.out.println("Post content for " + request.getUri() + " : " + contentStr);

            String newBody = contentStr.replace("e", "ei");

            ByteBuf bodyContent = Unpooled.copiedBuffer(newBody, CharsetUtil.UTF_8);

            contentBuf.clear().writeBytes(bodyContent);
            HttpHeaders.setContentLength(request, newBody.length());
        }
    }

    return null;
}

Do you know any other good example?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Gilles
  • 357
  • 3
  • 20

1 Answers1

1

You can for example remove specific headers like this:

@Override
public HttpResponse proxyToServerRequest(HttpObject httpObject)
{
    if (httpObject instanceof HttpMessage)
    {
        HttpHeaders headers = ((HttpMessage)httpObject).headers();
        headers.names().forEach(h -> headers.remove(headerName));
    }

    return super.proxyToServerRequest(httpObject);
}
Michal
  • 384
  • 3
  • 8