user1038795
user1038795

Reputation: 51

Removing utf-8 identifier (BOM) from Response Sent by WCF

I am creating a clone of the facebook Rest API in c#, I am testing it with the facebook PHP sdk. The problem I am having is that the responses sent by my net Rest Service contain utf-8 Bom in front of it and Facebook SDK is not able to parse the responses correctly. Any ideas on how to resolve this problem.

Upvotes: 5

Views: 2788

Answers (3)

kurt
kurt

Reputation: 653

I don't know what you are returning in your service, but if it is a string like mine (I was returning a json), you can simply return a Message object instead with the string in it (from System.ServiceModel.Channels - google it) and then at the end of your service method implementation just do this:

Encoding utf8 = new System.Text.UTF8Encoding(false); //false == no BOM
return WebOperationContext.Current.CreateTextResponse(stringWithContent, "application/json;charset=utf-8", utf8);

Upvotes: 2

Scott Rippey
Scott Rippey

Reputation: 15810

If you can specify a specific Encoding to your service, then you can use new UTF8Encoding(false) which is UTF-8 without BOM.

Upvotes: 4

sarnold
sarnold

Reputation: 104090

The Wikipedia UTF-8 article suggests that the pretend-BOM that Windows applications frequently prepend to the actual content is three bytes long. Can you simply not send the first three bytes of your generated content?

Upvotes: -1

Related Questions