This topic contains 3 replies, has 2 voices, and was last updated by
March 4, 2014 at 2:17 pm #13526
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
March 4, 2014 at 4:22 pm #13536MemberPoints: 0Rank: Member
Assuming there's no other way of getting around this (perhaps by double-escaping the slashes or something), I'd probably write a proxy function for Invoke-RestMethod which temporarily sets that flag, then sets it back when finished. That would limit the impact to the single pipeline where Invoke-RestMethod is called. Basically the same thing you've described, except the person calling Invoke-RestMethod doesn't have to do the work; they can just call the function the same way they would call the cmdlet.
March 5, 2014 at 2:47 am #13546
Brilliant idea Dave. That will much simplify the code in functions. I am thinking about adding new parameter to Invoke-RestMethod cmdlet called -AllowEscapedDotsAndSlashes, which when set will apply the hack to make call possible.
Thanks a lot for the suggestion, very much appreciated. And I have learned something new again 🙂
March 6, 2014 at 12:11 pm #13611
For anyone interested, the proxy function over Invoke-RestMethod which allows for escaped dots and slashes: http://mariuszwojcik.wordpress.com/2014/03/06/invoke-restmethod-cmdlet-proxy-more-on-un-escaping-forward-slashes-in-uris/
Thanks again, Dave
The topic ‘Need you opinion on a hack’ is closed to new replies.