in reply to LWP::UserAgent and correcting automatic redirection.

Just hook your code into the response_done or response_redirect handler, and modify the response there. See LWP::UserAgent.