Search code examples
asp.netjsonasp.net-mvc-4fiddler

Fiddler HttpRequest Composer - json.stringify


I want to use Fiddler to compose a HTTPRequest to an ASP.Net MVC Controller/Action. However, in this request, I want to be able to pass a JSON object as an object, and not simply as key value pairs which map to the different arguments in the method. I want to be able to test passing a complex type as one argument in the Controller/Action method.

This can be done in JQuery using Json.stringify({{json here}}) which passes the object as an object to MVC, and doesn't parse it into KVPs. Once again, I'm just looking to be able to test the same behavior in Fiddler, if it's possible.

Fiddler Requestenter image description here


Solution

  • The answer to this was simpler than I thought. What Json.stringify does, at it's core, is to wrap the Json string into an object which then becomes one single solitary KVP object, which then gets auto mapped based on the members and values of the Json Object passed and cast into your complex type object argument in your controller/action.

    So, all I had to do was wrap the JSON object notation,

    {
          "FirstName": "Your",
          "LastName": "Mom",
          "Email": "yourmom@gmail.com",
          "FilmAndSiteUpdates": true,
          "CompanyUpdates": true
     }
    

    into a Json object "instance" so to speak by assigning it to a member by the same name of the method signature, like so...

    {
        "input":  {
              "FirstName": "Your",
              "LastName": "Mom",
              "Email": "yourmom@gmail.com",
              "FilmAndSiteUpdates": true,
              "CompanyUpdates": true
         }
    }
    

    ...and ASP.NET MVC does the implicit casting for you.