Search code examples
restpowershellhead

Unable to get response when Head method is used with Invoke-RestMethod


I am running the following within PowerShell with the Head method but I am not getting any response. The expected output is in encoded format.

Invoke-RestMethod -Uri 'http://localhost:2375/containers/45e97476b2f5/archive?path=/home/test/' -Method Head

When I run the same REST API request with CURL or SOAP UI, I get expected output as below.

HTTP/1.1 200 OK
Content-Type: application/x-tar
Server: Docker/1.12.3 (linux)
X-Docker-Container-Path-Stat: eyJuYW1lIjoidGVzdCIsInNpemUiOjYsIm1vZGUiOjIxNDc0ODQxNDEsIm10aW1lIjoiMjAxNy0wMy0wN1QyMzoyMToyNC40MD

Any help?


Solution

  • Invoke-RestMethod is for interacting with a Restful web service and as such it does not return the headers of your request even when you specify the Head method.

    You should instead use Invoke-WebRequest which does bring back the headers, and other properties (with headers then being in the .headers property).

    For example:

    $Web = Invoke-WebRequest -URI 'http://localhost:2375/containers/45e97476b2f5/archive?path=/home/test/' -Method Head
    $Web.Headers