Send POST request to asp.net mvc action via Fiddler

Eels Fan picture Eels Fan · Jun 20, 2013 · Viewed 46.6k times · Source

I have an ASP.NET MVC web site. One of my routes is a URL that takes 5 parameters. For the sake of illustration, these parameters are named parameter1, parameter2, parameter3, parameter4, and parameter5. Currently, I'm constructing a URL in some C# code that will POST to the mvc action via a WebClient. that code looks like this:

WebClient myWebClient = new WebClient();
myWebClient.UploadStringCompleted += myWebClient_UploadStringCompleted;

string url = "http://www.example.com/customer/" + parameter1 + "/orders/" + parameter2 + "/" + parameter3 + "/" + parameter4 + "/" + parameter5;
myWebClient.UploadStringAsync(new Uri(url, UriKind.Absolute));

I'm confident that the UploadString method does a POST. I need to do a POST, because my parameter values can be very long. In fact, I estimate that occasionally, the total url length may be 20000 characters long. Regardless, I get a 400 error when I attempt to post my data. In an effort to debug this, I'm trying to figure out how to simulate a POST in Fiddler.

Assuming that I am passing values via a query string as shown above, what values do I enter into Fiddler? From the Composer tab, I'm not sure what to enter for the Request Headers area. I'm also not entirely sure what to enter for the url. I'm not sure if I put the entire URL in there, including the parameter values, or if those belong in the Request Headers.

What I need to enter into Fiddler, so that I can debug my issue?

Answer

Andrei picture Andrei · Jun 20, 2013

Basically all your parameters are a part of the URL, and this is the root of your problem. Here is what is going on: you are hitting the URL length limitation, and receiving a "400 Bad request" error. In real world most web browsers do not work with URLs more than 2000 characters long.

To resolve this problem I would suggest doing a bit of refactoring, so that request is posted to the URL http://www.example.com/customer/parameter1/orders or even http://www.example.com/customer/orders with parameters send in request body. Here is how test such request in Fiddler:

  1. On Composer tab choose POST request verb
  2. Specify the URL as

    http://www.example.com/customer/parameter1/orders

    or

    http://www.example.com/customer/orders

  3. In Request Headers section you can set content type header like

    Content-Type: application/x-www-form-urlencoded
    

    or any other header you might require. Or just leave it blank which will work in your case.

  4. Finally in Request Body field list your parameters in query string form

    parameter1name=parameter1value&parameter2name=parameter2value
    

In this new case here is how you can send such a request using WebClient:

WebClient myWebClient = new WebClient();
myWebClient.UploadStringCompleted += myWebClient_UploadStringCompleted;

string url = "http://www.example.com/customer/orders";
string data = "parameter1name=parameter1value&parameter2name=parameter2value";

myWebClient.UploadStringAsync(new Uri(url, UriKind.Absolute), data);