PowerShell Rest API Tips

REST APIs have become the bread and butter of todays cloud-first, mobile-first world. Such was my case recently when I was working on a small personal project, using some REST APIs but with PowerShell instead of the more common languages, like python, java, swift and so forth.

While working with the native Posh REST cmdlet: Invoke-RestMethod I saw that for a specific API I needed to disable ETag check by sending a If-Match request header using a wildcard: If-Match: "*"

Although this seems straight forward at first, after an hour of combing the interwebs and stackoverflow. I saw that there were no examples of this header check in powershell or documentation as to how this is done.

This led to me to try and figure it out on my own. Eventually i was able to figure how to add that If-Match header request in the Invoke-RestMethod headers parameters field. This can be seen in the code sample below:

Invoke-RestMethod -Uri "$apiUrl" `
-Headers @{Authorization=("Basic {0}" -f $auth); "If-Match"=("*")}

For the If-None-Match request header the code sample is below:

Invoke-RestMethod -Uri "$apiUrl" `
-Headers @{Authorization=("Basic {0}" -f $auth); "If-None-Match"=("*")}

In the end i thought this might help some other poor soul struggling with this issue.