Modifying the Request Body

To modify the request body in the pre-processing stage employ the content source and content producer to read from and write to the HTTP request.

Procedure

  1. Get Content source from request.
  2. Get input stream from Content source.
  3. Read content from the input stream.
  4. Modify the content.
  5. Create a content producer.
  6. Set the request body to the created content producer.
    ContentSource body = event.getCallContext().getRequest().getBody();
    final InputStream inputStream = body.getInputStream() ;
    //use input stream to read content
    //modify content
    httpReq.setBody(new ContentProducer() { //set new content body
        .........
        public void writeTo(OutputStream out) throws IOException {
            out.write("modified content")
            out.flush();
            out.close();
        }
    });
    Note:

    Refer to the working code in examples/ModifyRequestBody.java.