Handling POST with HttpListener

From IronPython Cookbook

If you want a longer introduction to System.Net.HttpListener, then visit Simple Server. This recipe (by Konrad), shows how to handle POST requests when writing servers using the HttpListener.

from System.IO import StreamReader
from System.Web import HttpUtility 

# Having HttpListenerContext in context
body = context.Request.InputStream
encoding = context.Request.ContentEncoding
reader = StreamReader(body, encoding)
nameValuePairs = HttpUtility.ParseQueryString(reader.ReadToEnd(), encoding)

# nameValuePairs contains now 
# a dictionary-like object ready for further processing

HttpUtility.ParseQueryString parses the query string (the body of the post) from the request, into name-value pairs.

Back to Contents.