RamblingCookieMonster / RabbitMQTools

PowerShell module containing cmdlets to manage RabbitMQ.
http://ramblingcookiemonster.github.io/RabbitMQ-Intro/
MIT License
31 stars 22 forks source link

Question: Content-length for Invoke-Restmethod #32

Open bf1967 opened 6 years ago

bf1967 commented 6 years ago

I'm looking for some enlightenment here if someone is so inclined to help me.

In the proxy for Invoke-Restmethod there is the following code

#By default the content-length is -1, which prevents ['Body'] from setting the content length.
if($PSBoundParameters['Body']) {
    if ($PSBoundParameters['Headers']) {
        $PSBoundParameters['Headers']['content-length'] = 0
    } else {
        $PSBoundParameters['Headers'] = @{ 'content-length' = 0 }
    }
}

which was introduced by @atomicblom in commit 897682cb1646fb358cb11f925795332d843b1c25 The commit message ("Improve Invoke-RestMethod handling for talking to localhost") is confusing, as these changes don't seem to have anything to do with that.

I ended up here after debugging the following error when calling

Add-RabbitMQQueue -Credentials $credRabbit -Name $QueueName -Durable
Invoke-RestMethod : You must write ContentLength bytes to the request stream before calling [Begin]GetResponse.
At C:\Program Files (x86)\WindowsPowerShell\Modules\rabbitmqtools\1.2\Public\Add-RabbitMQQueue.ps1:115 char:17
+ ...             $result = Invoke-RestMethod $url -Credential $Credentials ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], ProtocolViolationException
    + FullyQualifiedErrorId : System.Net.ProtocolViolationException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

I was able to get things working by removing the statement in the else clause. Does someone have an idea why the content length is set to zero? I've tried to find the origin of the comment "#By default the content-length is -1, which prevents ['Body'] from setting the content length." to determine the reason for this code.

Enquiring minds want to know.

Additional Details

Powershell version: Major Minor Build Revision

5 1 14393 1944

Occurs on both: Windows Server 2016 (version 1709) & Windows 10 (version 1607)

AtomicBlom commented 6 years ago

I'm trying to remember how this went as it was an issue I faced in a previous job so I don't have access to their issue tracker to refresh my own memory.

As I recall, I was faced with an issue while making our test environment scriptable. I believe the problem was that when it was calling the service against my local instance of RabbitMQ, the call was failing because the content length was being sent through explicitly as -1, rather than calculating the size automatically.

Setting the content length to 0, caused underlying HTTP library to calculate the length automatically.

Attempting to read up on the issue, suggests to me that perhaps it might have been a compatibility issue with Fiddler (hence why it was only failing on localhost?)

Given the dates of these commits, it's also quite possible that this was fixed/broken(?) in Powershell 5.0 or 5.1.

I have a feeling that the commits were cherry picked from my fork of the repository as I can't find any Issue or PR related to these commits, which makes me think it was a problem I was having on my PC and not on my workmates.

Hope that helps.

bf1967 commented 6 years ago

Thanks for the context @AtomicBlom. My sense was that it may have been something that was fixed in later versions of PS.

anotherjarvi commented 5 years ago

I was having a similar issue when running get-rabbitmqmessage. FWIW, I commented out the 'else' clause and it ran fine.