0

Trying to query API. Works fine in postman. When I try to run in Powershell, I get error:

Invoke-RestMethod : The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.

I know it returns:

Connection: close
Content-Encoding: gzip, deflate
Content-Type: application/xml; charset=utf-8
Content-Length: 11778
Date: Fri, 07 Jul 2023 12:49:25 GMT

I'm not familiar with GZIP on API calls. Could use some assistance. my script is

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$userAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $token")
$headers.Add("Accept", "*/*")
$headers.Add("Accept-Encoding", "gzip, deflate")
$headers.Add("Host", "b-fleet03.bellequipment.com:8080")

$response3 = Invoke-RestMethod 'https://b-fleet03.bellequipment.com:8080/Fleet' -Method 'POST' -Headers $headers -ContentType "text/plain; charset=utf-8" -UserAgent $userAgent

#$response3 | ConvertTo-Json
$response3 

I've tried variations of this:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$userAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $token")
#$headers.Add("Content-Length", "0")
#$headers.Add("User-Agent", "[Microsoft.PowerShell.Commands.PSUserAgent]::Chrome")
#$headers.Add("Content-Type", "application/xml; charset=UTF-8")
$headers.Add("Accept", "*/*")
  ([System.Net.HttpRequestHeader]::AcceptEncoding, "gzip")
$headers.Add("Accept-Encoding", "gzip, deflate")
$headers.Add("Host", "b-fleet03.bellequipment.com:8080")

#$headers.Add("Connection", "keep-alive")


$response3 = Invoke-RestMethod 'https://b-fleet03.bellequipment.com:8080/Fleet' -Method 'POST' -Headers $headers -ContentType "text/plain; charset=utf-8" -UserAgent $userAgent
#$response3 | ConvertTo-Json
$response3 

This is the successful query through postman:

POST https://b-fleet03.bellequipment.com:8080/Fleet
200
1633 ms
Network
addresses: {…}
tls: {…}
Request Headers
loco: besa
Authorization: Bearer XXXXXXXXX
User-Agent: PostmanRuntime/7.32.3
Accept: */*
Postman-Token: 29e59bd1-8d66-4140-9d10-90b3447e9694
Host: b-fleet03.bellequipment.com:8080
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 0

Response Headers
Connection: close
Content-Encoding: gzip, deflate
Content-Type: application/xml; charset=utf-8
Content-Length: 11779
Date: Mon, 10 Jul 2023 13:55:20 GMT
  • Can you show the raw request being sent by postman? Your Powershell version is using ```-Method 'POST'``` but not specifying a body - that’s not necessarily an error, but it’s unusual - are you sure your Powershell is identical to the postman version? – mclayton Jul 07 '23 at 22:40
  • Where are you adding the body to the Invoke-rest? Looks like you want to add xml data to the body that will get compressed using gzip. See following for "-body" option : https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7.3 – jdweng Jul 08 '23 at 09:55
  • It looks like one potential reason for this error is if the response claims to be GZip-encoded, but isn't - see [this answer](https://stackoverflow.com/a/30085593/45375). – mklement0 Jul 08 '23 at 12:55
  • @mclayton here is what is being sent by postman: POST https://b-fleet03.bellequipment.com:8080/Fleet 200 1633 ms Network addresses: {…} tls: {…} Request Headers loco: besa Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6XXXXXXXXX User-Agent: PostmanRuntime/7.32.3 Accept: */* Postman-Token: 29e59bd1-8d66-4140-9d10-90b3447e9694 Host: b-fleet03.bellequipment.com:8080 Accept-Encoding: gzip, deflate, br Connection: keep-alive Content-Length: 0 Response Headers Connection: close Content-Encoding: gzip, deflate Content-Type: application/xml; charset=utf-8 Content-Length – user3232808 Jul 10 '23 at 14:23
  • 1
    I can't really decode that is a comment with word wrapping - can you add it to your question as a code block? As mklement0 suggests, it might be a response issue with the stream not actually being gzipped despite the server claiming it is in the headers... – mclayton Jul 10 '23 at 15:28
  • @mclayton I've just added the code to my question.. thank you very much for your assistance. – user3232808 Jul 10 '23 at 15:37

0 Answers0