2

When using:

        $body = @{    
            Manager  = "spmigrationuser@contoso.com" #$item.PMEmail
            Name     = "some name"
            Number   = "Some number"
            Practice = "Some Practice"
        } 
$response = Invoke-RestMethod -Method Post -Uri $Url  -Body $body -ContentType 'application/json' # -Headers $Headers  

Or

$response = Invoke-WebRequest -Method 'POST' -Uri $Url  -Body $body -ContentType 'application/json' # -Headers $Headers  

Neither ContentType 'application/json' Nor $Headers = @{'Content-Type' = 'application/json' } -Headers $Headers
Works

The error is always:

"Invoke-WebRequest : {"error":{"code":"InvalidRequestContent","message":"The request content is not valid and could not be deserialized: 'Error parsing NaN value. Path '', line 1, position 1.'."}}"

The same call works in Postman

I am using PS 5.1 and I must have -ContentType 'application/json' otherwise PS works but the service fails

What can be the issue?

Ofer Gal
  • 707
  • 1
  • 10
  • 32
  • Are you passing the same headers in postman? What response body do you get back in postman? – Patrick87 Mar 12 '21 at 19:25
  • The same 'Content-Type' = 'application/json' and Postmant adds Content length and Host. I get 202 and it works How can I add the Content length and Host? – Ofer Gal Mar 12 '21 at 20:07

3 Answers3

1

I agree with NickSalacious. Your issue is that you are not sending JSON.

If you are using Postman and just starting to do API in PowerShell. Postman has a "Code" Link in the top right hand corner of the request. Just below and to the right of the Send button. In there you can select PowerShell. This will give you a good basis to see how the same request could be ran in PowerShell.

Postman would turn your body into this:

$body = "{`n    `"Manager`": `"spmigrationuser@contoso.com`",`n    `"Name`": `"some name`",`n    `"Number`": `"Some number`",`n    `"Practice`": `"Some Practice`"`n}"

This is not the easiest to work with and to read. Learning and using ConvertTo-Json is going to help a lot more in the long run.

*Edit: Also look at Invoke-RestMethod and Invoke-WebRequest. They behave differently and sometimes one will be better than the other.

*Edit2: Figured I would put an example of another way to do it.

$request = @{
Uri         = 'http://YourURI.Here'
Headers     = @{ 'Authorization' = $token
                'AnotherHeader?' = 'Sure'}
Method      = 'POST'
Body = '{    
        "Manager": $item.PMEmail,
        "Name": "some name",
        "Number": "Some number",
        "Practice": "Some Practice"
        }'
ContentType = 'application/json'
}
$response = Invoke-RestMethod @request
Redsloth55
  • 61
  • 2
  • Tried all these ways but keep getting {"error":{"code":"InvalidRequestContent","message":"The request content is not valid and could not be deserialized: 'Unable to | translate bytes [A0] at index 86 from specified code page to Unicode.'."}} – Ofer Gal Mar 13 '21 at 00:18
0

The API requires that the body be a JSON string. You can do a simple conversion (using ConvertTo-Json) in your Invoke-RestMethod command and set the content type accordingly.

Invoke-RestMethod -Method POST -Uri $uri -Header $header -Body ($body | ConvertTo-Json) -ContentType 'application/json'
0

Sorry I bothered all of you. I tested on another computer and it works fine.

Ofer Gal
  • 707
  • 1
  • 10
  • 32