Skip to content

invoke-restmethod and invoke-webrequest Content-Length incorrectly calculated #8736

@jonnords

Description

@jonnords

Steps to reproduce

I'm taking an UTF-8 encoded JSON and uploads the data to a qnamaker.ai instance.
Of a couple of 100 entries a few of them were failing and after some testing it seems to be due to the contentlength being incorrectly calculated for those entries.

I attempted to use Chunked which works perfectly in PS 5.1 but fails in core, I also attempted to set the content length but it ended up with a request containing two content lengths in the same request.

$APIKey= @{"Ocp-Apim-Subscription-Key"= $subscriptionKey;"Transfer-encoding" = "Chunked"}

Invoke-WebRequest -Method Put -Headers $APIKey -Uri $uri -ContentType "application/json" -Body $publish -skipheadervalidation

Expected behavior

I'm not certain why Chunked transfer encoding is failing in Core but not in the same script running 5.1 .

If I define my own Content-Length the invoke method should not include it's own Content-length. Alternatively use a more exact measure of length. I think what I hit is described here for node.js https://stackoverflow.com/questions/18692580/node-js-post-causes-error-socket-hang-up-code-econnreset/18693340#18693340

Actual behavior

Chunked requests are failing in Core 6.1 works in PS 5.1
Category : InvalidOperation
Activity : Invoke-RestMethod
Reason : HttpResponseException
TargetName : Method: PUT, RequestUri:
'https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/knowledgebases/', Version: 2.0,
Content: System.Net.Http.ByteArrayContent, Headers:
{
Ocp-Apim-Subscription-Key: 86418e3c2dfd420d80fb13857539608b
Transfer-Encoding: Chunked
User-Agent: Mozilla/5.0
User-Agent: (Windows NT 10.0; Microsoft Windows 10.0.17763; en-US)
User-Agent: PowerShell/6.1.1
Content-Type: application/json; charset=utf-8
}
TargetType : HttpRequestMessage

Content-Length is being represented twice although calculated separate from the cmdlet.
{
Ocp-Apim-Subscription-Key: 86418e3c2dfd420d80fb13857539608b
User-Agent: Mozilla/5.0
User-Agent: (Windows NT 10.0; Microsoft Windows 10.0.17763; en-US)
User-Agent: PowerShell/6.1.1
Content-Length: 1401
Content-Length: 5604
Content-Type: application/json
}
TargetType : HttpRequestMessage

Environment data

PS 6.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions