I have a PowerShell script to verify the existence of many (half a million) files in a web server. Some of these files are large (gigabytes) and I don't wan't to transfer them, so I've been using the HTTP HEAD method.
The problem is, today I found that the HTTP status code returned from the HEAD method may be different from the status returned by GET (which is always the correct one):
Using HEAD:
> Invoke-WebRequest -Method head http://example.com/82833749.mbtiles
StatusCode : 200
StatusDescription : OK
Content : {}
RawContent : HTTP/1.1 200 OK
Content-MD5: LwIBDR5QRptNAE2Hdpu+aw==
x-ms-request-id: 774da565-101e-0053-7903-38e136000000
x-ms-version: 2009-09-19
x-ms-lease-status: unlocked
x-ms-blob-type: BlockBlob
Connect...
Headers : {[Content-MD5, LwIBDR5QRptNAE2Hdpu+aw==], [x-ms-request-id,
774da565-101e-0053-7903-38e136000000], [x-ms-version, 2009-09-19], [x-ms-lease-status,
unlocked]...}
RawContentLength : 0
With GET:
> Invoke-WebRequest http://example.com/82833749.mbtiles
Invoke-WebRequest : BlobNotFoundThe specified blob does not exist. RequestId:dede785b-b01e-0091-4b08-38a7b0000000
Time:2022-03-15T01:02:36.7883837Z
No linha:1 caractere:1
+ Invoke-WebRequest http://example.com/82833749.mbtiles
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
> $Error[0].Exception.Response.StatusCode.value__
404
Right after running the GET request, the HEAD request will return the correct status code, making it more confusing to understand what is going on.