Is it possible to redirect with POST data?

5,287 views
Skip to first unread message

Bob T.

unread,
May 3, 2007, 4:24:06 PM5/3/07
to Django users
Hi All,

I have a situation where I would like a view to do a redirect to a URL
that is expecting POST (rather than GET) data. GET would be easy, but
how can I do a POST redirect? The URL I would be redirecting to is not
under my control, so I can't just change it to use GET data.

Thanks,
Bob

Michael K

unread,
May 3, 2007, 4:36:25 PM5/3/07
to Django users

Bob,

It entirely depends on what you're trying to do, but as you've
described it, the only way I know of is using JavaScript on the view
page that submits the POST data.

If the target site was under your control, there'd probably be other
ways of doing it.

If I were an expert in JavaScript, I'd give you an example, but I
believe HttpRequest is the place to start.

HTH,

Michael


Joseph Heck

unread,
May 3, 2007, 4:36:36 PM5/3/07
to django...@googlegroups.com
No - redirect sends information back to the browser that causes the browser to make another request - GET style attributes can be easily included in there, but the "aside" kind of data that you have in a POST is not sent back.

The closest you could get from a server side type of operation is some sort of dynamic proxy, but that is probably far more complicated than you want. (at a guess)
-
joe

Mike Axiak

unread,
May 3, 2007, 5:08:51 PM5/3/07
to Django users
If it's not necessary to have the client *actually* go to the page,
you could so some shady things with httplib [1].

E.g.
import httplib, urllib
params = urllib.urlencode(request.POST)
headers = {"Content-type": "application/x-www-form-urlencoded",
"Referer": "http://google.com"}
conn = httplib.HTTPConnection("funsite.com:80")
conn.request("POST", "/interesting-place", params, headers)
response = conn.getresponse()
if response.status == 200:
data = response.read()
conn.close()
return HttpResponse(data)
conn.close()
raise Http404


This is a very rough sketch of the shadiness you can do.

Cheers,
Mike Axiak

1: http://docs.python.org/lib/module-httplib.html

On May 3, 4:24 pm, "Bob T." <btaylor...@gmail.com> wrote:

Bob T.

unread,
May 4, 2007, 11:25:43 AM5/4/07
to Django users
Thanks, guys. That gives me a good start on handling the problem!


David Larlet

unread,
May 4, 2007, 3:46:04 PM5/4/07
to django...@googlegroups.com
2007/5/3, Bob T. <btayl...@gmail.com>:

In a RESTful goal, I'd like to do the same and I'm really interested
in any solution.

Regards,
David

Reply all
Reply to author
Forward
0 new messages