Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServiceStack deserialization failing on jQuery request but not C# client

My frontend implementer is running into a strange issue with a rather complex nested DTO.

Here is the json that he is passing through a jquery ajax call

{"Id":"507e7e5aa6305825c012c606","Name":"test fried chicken","Description":"why you no work","Servings":4,"Author":"unit test","Steps":[{"Instructions":"put chicken in egg","Ingredients":[{"PreparationId":"507e7e34a6305825c012c601","MeasureId":"kilogram","Quantity":2,"MeasureType":"weight"},{"PreparationId":"507e7e36a6305825c012c605","MeasureId":"gram","Quantity":100,"MeasureType":"weight"}]},{"Instructions":"put in flour and cook it up","Ingredients":[{"PreparationId":"507e7e35a6305825c012c603","MeasureId":"gram","Quantity":100,"MeasureType":"weight"}]}]}

That is going as a PUT to uri/recipes which then being converted on the request side to a default instance of the DTO (none of the information is actually being deserialized).

If I take the exact same Json from the request and run the C# code

var d = new ServiceStack.Text.JsonSerializer<RecipeDTO>();
var re = d.DeserializeFromString(theJson);
var client = new JsonServiceClient(uri);
client.Put<RecipeDTOResponse>("/recipes", re); 

then it works. Any ideas why there is a difference?

like image 883
mikkelfishman Avatar asked Jan 19 '26 09:01

mikkelfishman


1 Answers

After logging the IHttpRequest coming from both the webpage and the C# client, I noticed that ContentType for the jquery call was application/x-www-form-urlencoded while c# was application/json.

Changing it in the jquery call resolved the issue.

like image 142
mikkelfishman Avatar answered Jan 20 '26 23:01

mikkelfishman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!