10

OK,

I am using the System.Runtime.Serialization and the DataContractJsonSerialization.

The problem is that in the request I send a value of a property with the & character. Say, AT&T, and I get a response with error: Invalid JSON Data.

I thought that the escaping would be done inside the library but now I see that the serialization is left untouched the ampersand & character.

Yes, for a JSON format this is valid. But it will be a problem to my POST request since I need to send this to a server that if contains an ampersand will response with error, hence here I am.

HttpUtility.HtmlEncode is in the System.Web library and so the way to go is using Uri.EscapeUriString. I did this to try, but anyway, and without it all requests are working fine, except an ampersand is in a value.

EDIT: HttpUtility class is ported to the Windows Phone SDK but the prefer way to encode a string should be still Uri.EscapeUriString.

First thought was to get hands dirty and start replacing the special character which would cause a problem in the server, but, I wonder, is there another solution I should do, that it would be efficient and 'proper'?

I should tell that I use

// Convert the string into a byte array.
byte[] postBytes = Encoding.UTF8.GetBytes(data);

To convert the JSON to a byte[] and write to the Stream. And,

request.ContentType = "application/x-www-form-urlencoded";

As the WebRequest.ContentType.

So, am I messed up for a reason or something I miss?

Thank you.

5
  • 1
    You say that you're sending JSON data, but the content-type says otherwise (application/x-www-form-urlencoded). Can you post more information about how you're sending the data, and how the service is receiving it? Sep 9, 2013 at 22:36
  • Even changing it, the server returns 500. Sep 9, 2013 at 22:49
  • What do you use on the server? Can you post the server code? Sep 9, 2013 at 23:01
  • No i have no access on the server. It's a really strange problem. This should just work. I guess something's wrong with the server, I'll contact the guy handles it. Sep 9, 2013 at 23:27
  • Yes, you need to contact the server owner. 500 (Internal Server Error) means something is wrong with the server. Sep 9, 2013 at 23:35

2 Answers 2

11

The problem was that I was encoding the whole request string including the key. I had a request data={JSON} and I was formatting it, but the {JSON} part should only be encoded.

string requestData = "data=" + Uri.EncodeDataString(json) // worked perfect!

Stupid hole to step into.

3
  • Interesting. Thanks for sharing.
    – weiglt
    Sep 10, 2013 at 22:17
  • No problem. It solves my case, you never know who else could miss something similar. Actually it's a combination client-server contract. The server searches if there is a key "data" in the body and if not takes the whole body trying to deserialize it, then in my case it failed due to ampersand character trying to separate as a parameter. Sep 13, 2013 at 13:23
  • It's Uri.EscapeDataString() not Encode...Unless I'm missing something. Jul 6, 2021 at 19:22
4

Have you tried replacing the ampersand with & for the POST?

5
  • If I replace & with & it fails as well. Sep 9, 2013 at 22:17
  • You should never include a regular ampersand in any kind of URL request payload, but always encode it. So i'd say, yeah, you have to get busy there and do some coding. Maybe have a look at: stackoverflow.com/questions/11294107/…, encodeURIComponent() seems to work.
    – weiglt
    Sep 9, 2013 at 22:19
  • I don't mind that, I just wanted to make sure that this is the case. And how would a replace should be done for the other side to get the proper value? I use Uri.EscapeUriString for encoding. Sep 9, 2013 at 22:21
  • This is C#, not Javascript. Sep 9, 2013 at 22:32
  • No problem, I think this is a server side issue, it just doesn't work with any encode that I apply to the JSON string. Sep 9, 2013 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.