⏮️ Back to AssemblyScript README.md
This application modifies the body in onRequestBody and onResponseBody functions.
-
In
onRequestHeadersit resets thecontent-lengthheader. This is required as we are going to alter the body content. -
In
onRequestBodyit reads in the body fromget_buffer_bytesand checks if it contains the termClient, if so it alters the body usingset_buffer_bytes. -
In
onResponseHeadersit resets thecontent-lengthheader, sets thetransfer-encodingheader and ensures that the runtime propertyresponse.content_typeis set according to what it has received in the headers. -
In
onResponseBodyit finally reads and logs the known properties and the returns the body content.
This demonstrates the basic flow of how to manipulate the body in each life cycle hook, be mindful that altering the body requires you to manipulate the required headers before doing anything.
Also notice that we wait on the end_of_stream bool to be true before processing the body. This allows it to pass through multiple invocations buffering the content before we do any manipulation. ( you could also process this data in chunks if needed )