I am trying to send a large chunk of data over to a HTTP handler. I can't send it using GET because of the URL length limit so I decided to POST it instead. The problem is that I can't get at the values. context.Request.Form shows that it has 0 items. So is there a way that I can POST data to a HttpHandler?
-
What are you using to make the request? HttpWebRequest? – John Sheehan May 26 '09 at 05:31
-
I am making an Ajax call using Jquery – Ali Kazmi May 26 '09 at 05:56
-
Could you provide a code sample of what you are doing on the request side? – Deeksy May 26 '09 at 06:25
-
I thought you are using pure Ajax without any wrapper. If you are using jQuery, then you'll get the values you POST params using the Request collection. And btw I was telling to POST the data in querystring FORMAT, not as a querystring. – Kirtan May 26 '09 at 06:59
-
ok I'll try that, though i am not very hopeful :) – Ali Kazmi May 26 '09 at 07:05
-
it would be helpful to show the jquery code, are you using the jquery post method, e.g: $.post("test.php", { name: "John", time: "2pm" } ); installing firebug in firefox can be very helpful for debugging ajax server interactions. – russau May 26 '09 at 12:43
6 Answers
Having some code to look at would help diagnose the issue. Have you tried something like this?
jQuery code:
$.post('test.ashx',
{key1: 'value1', key2: 'value2'},
function(){alert('Complete!');});
Then in your ProcessRequest()
method, you should be able to do:
string key1 = context.Request.Form["key1"];
You can also check the request type in the ProcessRequest() method to debug the issue.
if(context.Request.RequestType == "POST")
{
// Request should have been sent successfully
}
else
{
// Request was sent incorrectly somehow
}

- 99,428
- 48
- 189
- 219
I also had the same problem. It was an client/AJAX problem. I had to set the AJAX call request header "ContentType" to
application/x-www-form-urlencoded
to make it work.

- 102,760
- 52
- 202
- 249
I was having the same problem, and eventually figured out that setting the content type as "json" was the issue...
contentType: "application/json; charset=utf-8"
That's a line some popular tutorials suggest you to add in the $ajax call, and works well with ASPx WebServices, but for some reason it doesn't for an HttpHandler using POST.
Hard to catch since values in the query string work fine (another technique seen in the web, though it doesn't makes much sense to use POST for that).

- 6,961
- 3
- 40
- 41
POST fields are contained in
HttpContext.Request.Params
To retrieve them you can use
var field = HttpContext.Request.Params["fieldName"];

- 2,467
- 1
- 25
- 23
Faced similar problem. After correcting all issues, there was one more thing I missed in web.config
- to change verb as *
OR GET,POST
. After that everything worked fine.
<httpHandlers>
...
<add verb="*" path="test.ashx" type="Handlers.TestHandler"/>
</httpHandlers>

- 3,508
- 5
- 26
- 42

- 11
- 1
The POST data that you are sending to your HTTP Handler must be in querystring format a=b&c=d
. And you can retrieve it on the server-side using Request["a"]
(will return b
), and so on.

- 21,295
- 6
- 46
- 61
-
Sorry, but I don't get it. How would the data be POSTed if I send it in a Querystring :S . Can u explain a bit more what you are suggesting? If u are saying that I should create a querystring and append that to my URL than that wouldn't work due to the URL length limit. – Ali Kazmi May 26 '09 at 06:00
-
I think he means to URL-encode the data, delimited by ampersands (&), just like in a query string, except in the request body, not in the URL. jQuery should handle this for you automatically. – Mark Cidade Jan 28 '11 at 21:21