Thomas B
Thomas B

Reputation: 311

Invoke-WebRequest - issue with special characters in json

I'm trying to send special characters (norwegian) using Invoke-WebRequest to an ASP .NET MVC4 API controller.

My problem is that the json object show up as NULL when received by the controller, if my json data contains characters like Æ Ø Å.

An example of my code:

$text = 'Æ Ø Å'
$jsondata = $text | ConvertTo-Json
Invoke-WebRequest -Method POST -Uri http://contoso.com/create -ContentType 'application/json; charset=utf8' -Body $jsondata

Also when looking in fiddler the characters turn up like the usual weird utf8 boxes.

Sending json data from fiddler to the same API controller works fine

Any advice?

Upvotes: 14

Views: 19088

Answers (1)

Keith Hill
Keith Hill

Reputation: 201632

For the Body parameter try this:

... -Body ([System.Text.Encoding]::UTF8.GetBytes($jsondata))

The string in PowerShell is Unicode but you've specified a UTF8 encoding so I think you need to give it some help getting to UTF8.

Upvotes: 41

Related Questions