How to intercept a request and send it with HttpClient to another server and return the response from that server after some modification?
The original request will contain data and files. I am looking for the least verbose solution, up to copying the original raw body as a string. The part I am stuck with is plugging the Request object to the HttpClient request in the shortest way (without writing code to copy data and headers bit by bit), the rest (handling the response) I will manage.
Symfony's Http Client does not accept an
HttpFoundation\Requestas a parameter to make new requests.You'll need to convert the
Requestobject into the necessary parameters (including converting the path, method, passing appropriate headers, etc) "manually".Then, you'd have to create the an
HttpFoundation\Responsebased on the client's response (which, again, cannot simply be sent back in your controller).Depending on your specific requirements, it's not particularly onerous. A naive, probably buggy implementation would be something like:
Again, this is very naive implementation. You'd need to make sure that you deal with any exceptions and edge cases.
Take into account that PHP is not a great choice of language to build a real HTTP proxy.