-1

I am posting a JSON object from my MonoTouch application to my Asp.MVC Controller. The JSON object has a few string parameters. One of the parameters has a base64 encoded string. The base64 string is a representation of an image.

Issue is: On the server, when the request comes in, sometimes I get this error:

Unterminated string passed in. (49152).  

What's worse is that this only happens on production, not dev.

Now, the number 49152, varies from request. My guess is that the JSON string is not getting fully posted. It's being cut off. I've tried using Fiddler to slow down my bandwidth, in case slow internet would be the cause. However, that was not the cause either because i couldn't reproduce the issue on dev with that.

Possible issues, I see:

  1. "\0" getting appended at the end of the Base64 string, as noted here: https://stackoverflow.com/a/710882/487940. I really don't think anything is wrong with the Base64 because the data is getting serialized in the MonoTouch client, so if anything was wrong, then it wouldn't serialized in the first place.
  2. MonoTouch HttpWebRequest not posting all the data. This is likely the issue IMO. The request being sent is gone bad.

Full Stack Trace:

   System.ArgumentException: Unterminated string passed in. (49152):
    .....LONG JSON STRING WHICH IS MISSING THE END **" }** and possible other characters.
     at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeString()
       at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)
       at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)
       at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)
       at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)
       at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)
       at System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext)
       at System.Web.Mvc.ValueProviderFactoryCollection.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory)
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext)
       at System.Web.Mvc.ControllerBase.get_ValueProvider()
       at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)
       at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
       at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

UPDATE

I was thinking that I would intercept the request in asp.net mvc before it gets routed to the controller. If I can intercept it, I can get the raw data and log it. Is there anyway to do this in Asp.Net MVC? As I pointed out above, I really don't think anything is wrong with the Base64 string. It has to be something with the request.

Community
  • 1
  • 1
harsimranb
  • 2,231
  • 1
  • 35
  • 55
  • Did you log the input JSON string what you got? Probably it might not have ended properly due to escape characters. Having that JSON string will give more information. – ramiramilu Jan 24 '14 at 19:54
  • thats what i will do, but this issue is happening in prod and the current version in prod does not log the JSON. the next release isn't do in a while. However, is there a way for me to intercept the request before it gets routed to the controller and deserialized? Because there i could get the raw data and log it. – harsimranb Jan 24 '14 at 22:13
  • 1
    You can use [Fiddler 2](http://fiddler2.com) on Windows running your server or [Wireshark](http://www.wireshark.org) on Mac OS X to intercept the HTTP connections. – Alexandre Marcondes Jan 25 '14 at 01:39
  • @ramiramilu So I'm using the JavaScriptSerializer in .Net to create the JSON. Asp.Net MVC also seems to be using this serializer to de-serialize the request. So would the JavaScript Serializer put escape characters during serialization and then fail during deserialization? Is that possible? – harsimranb Jan 27 '14 at 17:26
  • @AlexandreMarcondes I can't install anything on our prod server. I can put something on dev, but no one has been able to reproduce this on out dev. And this happens probably twice in a week on prod, not very frequent – harsimranb Jan 27 '14 at 17:28
  • Well @Pathachiever11 I guess I would increase the log verbosity for a week so that I have a dump of every resquest/response if I were you. – Alexandre Marcondes Jan 27 '14 at 19:31
  • Thanks @AlexandreMarcondes, I will talk to my team about it. I think even doing it for a day would help. This morning alone we have more than couple dozen of the errors. – harsimranb Jan 28 '14 at 01:04
  • Please, when possible, post here the logs and a better error description. If there is a solution you can answer your own question with it. – Alexandre Marcondes Jan 28 '14 at 01:55
  • @AlexandreMarcondes The stacktrace is what I got from the log. I can't really make out a better error description because I've posted everything to the best of my knowledge, even given my insights. If I have a solution, I will of course post it. – harsimranb Jan 28 '14 at 17:21
  • Why did this question get a -1 rating? – harsimranb Jan 28 '14 at 17:22

2 Answers2

0

Please, make sure that Content-Length or your request is passed correctly and corresponds to the size of your request body (in bytes).

antonv
  • 1,227
  • 13
  • 21
0

Instead of sending the base64 encoded string, I ended up implementing a MultiData Form Post, and sending the byte array. So far it seems this solves the problem.

harsimranb
  • 2,231
  • 1
  • 35
  • 55