Specifying a Custom Message-Body Handler
By default, RWHttpClient::getReply() returns an RWHttpReply object. The body of the message is stored as an RWCString internally and is accessible through the RWHttpReply::getBody() member function. You can specify an alternate mechanism for reading the body of the message from the underlying data connection by passing an RWTFunctor handler to the RWHttpClient::getReply() function.
Example 12 creates a function that reads from a portal and writes the associated data to a file.
Example 12 – Creating a function that reads from a portal
void writeToFile(RWPortal portal, RWCString filename) {
RWStreamCoupler coupler(RWStreamCoupler::binary); // 1
ofstream ostrm(filename); // 2
RWPortalIStream istrm(portal); // 3
couple(istrm, ostrm); // 4
}
//1 Constructs an RWStreamCoupler object that redirects the portal stream to the file stream. For more information on RWStreamCoupler, see About RWStreamCoupler.
//2 Constructs a file output stream from filename, where the body of the message is stored.
//3 Constructs an RWPortalIStream from the portal that was passed to the function. For more information on RWPortalIStream, see the Essential Networking User’s Guide.
//4 Couples the portal input and file output streams together so that all data that is read from istrm is written to ostrm.
After writing a function that can read from a portal and write the data to a file, you can create an RWTFunctor handler that uses the function to read the body of an HTTP reply, as shown in Example 13 .
Example 13 – Using an RWTFunctor handler
RWHttpClient client = RWHttpSocketClient::make();
// initialize, connect, and submit a request…
 
RWTFunctor<void(RWPortal)> handler; // 1
 
handler = rwBind(writeToFile, rw1, "request.out"); // 2
 
RWHttpReply reply = client.getReply(handler); // 3
//1 Creates an RWTFunctor<> object that is templatized on the first parameter type (RWPortal).
//2 Initializes the object handler with the function to execute and with the second parameter that is not supplied by the RWHttpClient::getReply() invocation.
//3 Invokes the getReply() member function on client. The reply returned from this invocation does not contain a body, which means that RWHttpReply::getBody() returns an empty string. However, the handler stores the message body in the file request.out.
NOTE: RWHttpAgent does not include a mechanism for specifying a body handler. If the body of a message requires special treatment (for instance, it cannot be stored temporarily in an RWCString), then your application must use RWHttpClient to retrieve the document.