Sudheej
Sudheej

Reputation: 2029

Invoke-RestMethod Powershell V3 Content-Type

I am able to use the JIRA rest API from powershell v5. However the same code throws the below error in powershell v3.

WARNING: Remote Server Response: The 'Content-Type' header must be modified using the appropriate property or method.

Source Code

$basicAuth = "Basic " + [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$($Username):$Password"))
    $headers = @{
        "Authorization" = $basicAuth
        "Content-Type"="application/json"
    }

 $response = Invoke-RestMethod -Uri $requestUri -Method POST -Headers $headers -Body $body

Upvotes: 5

Views: 26723

Answers (1)

Mark Wragg
Mark Wragg

Reputation: 23395

Invoke-Restmethod has a -ContentType parameter, so the error seems to be indicating you should use that to specify Content Type instead of passing it via the headers parameter:

$basicAuth = "Basic " + [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$($Username):$Password"))
    $headers = @{
        "Authorization" = $basicAuth
    }

 $response = Invoke-RestMethod -Uri $requestUri -Method POST -Headers $headers -Body $body -ContentType 'application/json'

Upvotes: 9

Related Questions