Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 590
Thomas B Avatar asked Mar 08 '13 09:03

Thomas B


1 Answers

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.

like image 66
Keith Hill Avatar answered Oct 21 '22 09:10

Keith Hill