1

I have an ASP.NET Web API service that accepts JSON data. It works perfectly for all of our JSON data types except for one. Users are able to send inspection results from their mobile devices. The inspection results are received by an ASP.NET Web API service as JSON.

I have unit tests written in C# that are able to send inspection JSON data without a problem. But whenever I send the data from a web page the JSON data that is sent seems to be getting truncated.

This is what is sent.

{"Formname":"inspection","Formdata":{"UserId":1011357,"InspectionId":40013,"VehicleReg":"AA123ABC","Results":[{"QuestionId":100053,"OptionId":30192,"OptionResponse":"fantastic"}]},"Profile":{"EmailAddress":"[email protected]","OscarId":"1011369"}};

This is what is received.

{"Formname":"inspection","Formdata":{"UserId":1011357,"InspectionId":40013,"VehicleReg":"AA123ABC","Results":[{"QuestionId":100053,"OptionId":30192,"OptionResponse":"fantastic"

It's getting truncated at a length of 254.

Here is the AJAX that creates the request.

var url = "http://mywebservice/api/routingtasks?formname=inspection";

        $.ajax({
            type: "POST",
            url: url,
            contentType: "application/json",
            headers: { "Authorization" : "TESTUSER " + signedToken},
            data: JSON.stringify(formdata),
            success: function(data){
                alert("Task successfully processed.");
            },
            error: function(error){
                alert("error: " + JSON.stringify(error));
            }
        })

The signature of the Web API controller that receives the JSON data is this.

public HttpResponseMessage RoutePostData(string formname, [FromBody] JToken postdata)

My C# unit tests are able to send the same JSON data without a problem, but sending the JSON data from a web page is causing a problem.

2
  • The truncation length may suggest an answer as 254 is no random number (byte). Is it possible something in the set up of your application is limiting the send buffer size? Commented Mar 22, 2017 at 14:21
  • That was my thought too when I saw the number 254. It looks that way, but I don't know what settings would affect the size of the JSON. Commented Mar 22, 2017 at 15:50

3 Answers 3

1

Your json is wrong. "Profile":{"EmailAddress" curly brace is missing. Not sure if it is copy paste error or your code error. However it truncates right after fantastic.

Change your json to

{"Formname":"inspection","Formdata":{"UserId":1011357,"InspectionId":40013,"VehicleReg":"AA123ABC","Results":[{"QuestionId":100053,"OptionId":30192,"OptionResponse":"fantastic"}]},"Profile":{"EmailAddress":"[email protected]","OscarId":"1011369"}}
Sign up to request clarification or add additional context in comments.

2 Comments

Can you check http post request body? what's getting sent? You can use fiddler or browser devtool network, to check exact payload being sent on wire. Just to cross check, if anything truncating request body on client side. Because your content length is 250 anyways. Content-Length: 250
According to Fiddler the entire JSON payload is getting sent
0

If formdata is already javascript object, you don't need to stringify it.

$.ajax({
            type: "POST",
            url: url,
            contentType: "application/json",
            headers: { "Authorization" : "TESTUSER " + signedToken},
            data: formdata,
            success: function(data){
                alert("Task successfully processed.");
            },
            error: function(error){
                alert("error: " + JSON.stringify(error));
            }
        })

You can read more on documentation page: http://api.jquery.com/jQuery.ajax/

Comments

0

Try this:

<configuration>
     <appSettings>
          <add key="aspnet:MaxJsonDeserializerMembers" value="5000" />
     </appSettings>
</configuration>

Set the value to something of your choice.

4 Comments

Does this value affect the deserialization of the incoming data over the HTTP request? Or does it only affect data that is manually deserialized in code?
It applies if you use the JavaScriptSerializer class.
This won't have any affect then. It appears that it's the HTTP request that is truncating the JSON as I can make exactly the same request from my C# unit test without a problem. So the issue must be how the web / browser / AJAX is composing the request.
Who knows. This kind of behavior however where something works on your unit test and not on the app itself is usually related to some environment difference. Did you try it? It's simple enough.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.