WCF Data Services supports Accept-Charset

I’m not sure if the documentation states this explicitly somewhere, but WCF Data Services supports the Accept-Charset header, and will try to pick a character set that works for the client that is making the request. .NET developers are probably more familiar with the term ‘Encoding’, which is the type name that encapsulates how we translate back and forth between bytes and strings.

The support has been around for a while. In fact, it gave us some grief a couple of years ago, but it’s all better now – the client actually leverages this to make sure it understands the server’s response.

If you want to play with this for a bit, try the following: start up Fiddler, then navigate to http://services.odata.org/OData/OData.svc/Products(0). You can then drag the request you get into the Request Builder tab and tweak it a little bit. For example, add a line that reads as follows.

Accept-Charset: utf-16

And then Execute the request. If you look at the response using the Hex View tab, you’ll see that characters now take up two bytes. You can try other encodings, like us-ascii, although the data is relatively simple and you won’t see many changes.

If you have localized data for which UTF-8 isn’t an appropriate encoding, you can use this capability to choose a different one, perhaps reducing the payload size or making it easier for a client or server to consume the data.


Comments (0)

Skip to main content