Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Content-length for Invoke-Restmethod #32

Open
bf1967 opened this issue Apr 3, 2018 · 3 comments
Open

Question: Content-length for Invoke-Restmethod #32

bf1967 opened this issue Apr 3, 2018 · 3 comments

Comments

@bf1967
Copy link

bf1967 commented Apr 3, 2018

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 897682c
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
Copy link

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
Copy link
Author

bf1967 commented Apr 9, 2018

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

@anotherjarvi
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants