Can the fiddler2 viewer be added to the request packet inspector

Aug 21, 2008 at 6:49 PM
I am writing applications that send and receive json, so it would be nice to be able to have the viewer work on the put/post as well as the response.
Oct 9, 2009 at 10:19 AM

I would like to second this request. As an RIA developer, most of my outgoing requests are json encoded RPC packets. It would be VERY useful if Json Viewer could inspect POST/PUT content for json data (or even parse inline variables in the request url for json-compatible data and allow inspection of those items too..)

Beyond that - thank you for an insanely useful plugin. It cuts my dev time on RI apps considerably and makes debugging async comms a breeze.

Nov 12, 2009 at 9:40 PM

Hey guys,

I third this request but took it upon myself to actually download the source code and create Request version of this plugin.  I've tested it and it works great.  Would it be possible for one of the developers to add this to the source code so it's incorporated into the official download?  

Keep in mind, the code was not refactored or cleaned up...it's pretty much just a straight up copy to get it working for my needs but now that it works, it works great!

So this file: JsonRequestInspector.cs just needs to be added to the FiddlerJsonViewer project all that is necessary is a recompile of the project.  

That's it! 

Cheers,

Ralph Caraveo

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using EPocalipse.Json.Viewer;
using Fiddler;
using System.Drawing;

namespace EPocalipse.Json.Fiddler
{
    public class JsonRequestInspector : Inspector2, IRequestInspector2
    {

        private byte[] _body;
        JsonViewer viewer;
        HTTPRequestHeaders _headers;

        public override void AddToTab(TabPage o)
        {
            viewer = new JsonViewer();
            o.Text = "JSON Request";
            o.Controls.Add(viewer);
            viewer.Dock = DockStyle.Fill;
        }

        public override int GetOrder()
        {
            return 0;
        }

        public override int ScoreForContentType(string sMIMEType)
        {
            if (string.Compare(sMIMEType, "text/json", true) == 0)
            {
                return 50;
            }
            return 0;
        }

        public override void SetFontSize(float flSizeInPoints)
        {
            viewer.Font = new Font(viewer.Font.FontFamily, flSizeInPoints, FontStyle.Regular, GraphicsUnit.Point);
        }

        #region IRequestInspector2 Members

        public void Clear()
        {
            _body = null;
            viewer.Clear();
        }

        public override void ShowAboutBox()
        {
            MessageBox.Show("Json Inspector 1.1\nCopyright (c) 2007 Eyal Post\nhttp://www.epocalipse.com/blog", "Json Viewer", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public bool bDirty
        {
            get { return false; }
        }

        public bool bReadOnly
        {
            get
            {
                return false;
            }
            set
            {
            }
        }

        public byte[] body
        {
            get
            {
                return _body;
            }
            set
            {
                _body = value;
                string json = Encoding.UTF8.GetString(_body);
                bool encoded = _headers.Exists("Transfer-Encoding") || _headers.Exists("Content-Encoding");
                if (_headers.ExistsAndEquals("Transfer-Encoding", "chunked"))
                {
                    //response in chuncked but still may contain the entire json string. Try to fix it.
                    json = json.Trim();
                    int startPos = json.IndexOf('\n');
                    int endPos = json.LastIndexOf('\r');
                    if (startPos >= 0 && endPos >= 0 && startPos != endPos)
                    {
                        json = json.Substring(startPos, endPos - startPos + 1);
                        json = json.Trim();
                    }
                }
                viewer.ShowTab(Tabs.Viewer);
                viewer.Json = json;
                if (viewer.HasErrors && encoded)
                {
                    viewer.ShowInfo("Error parsing JSON. Try using the Transformer to remove the encoding from this response");
                }
            }
        }

        public HTTPRequestHeaders headers
        {
            get
            {
                return null;
            }
            set
            {
                _headers = value;
            }
        }

        #endregion


    }
}

 

 

Jan 4, 2010 at 7:09 PM

Thank you so much for that contribution, I started down a path to do the same thing and during my research found this post!