I am working on a module for administering RabbitMQ servers. All communication with the server is through REST API, so I’m using Invoke-RestMethod extensively. Unfortunately, some methods require passing forward slash (/) in the url in it’s encoded form: %2f. As it turns out, in .NET Framework versions earlier than 4.5, the Invoke-RestMethod will automatically un-escape the forward slash back to /. After some research I found a way to hack that behaviour. It requires bit of reflection to modify internal flag on UriParser class, but the problem is that this will affect whole PowerShell session. I came with following way to tackle the problem:
- on module registration - detect if hack is needed, print warning to the user
- before invoking rest method which requires hack - modify internal flag
- after invoking rest method, in finally block - restore state before applying hack
- add verbose output whenever hack is applied
- add extra information in module's help explaining the problem
Can anyone think about what else I could do to minimise impact of the hack. What could be done in a better way, or perhaps there is better approach?
I wrote an article explaining how to prevent unescaping in url’s at http://mariuszwojcik.wordpress.com/2014/03/04/how-to-prevent-invoke-restmethod-from-un-escaping-forward-slashes/
the modules code is on GitHub: http://mariuszwojcik.wordpress.com/2014/03/04/how-to-prevent-invoke-restmethod-from-un-escaping-forward-slashes/
Files related to the problem are: PreventUnEscapeDotsAndSlashesOnUri.ps1 and AddExchange.ps1.
I much appreciate any input.
Thanks a lot, Mariusz