1

In Pyramid, send a request by GET can be done by create URL like this:

@view_config(route_name="test")
def test(request):
    return HTTPFound("http://test.com/redirect_to_another_test?a=1")

But it seems that the HTTPFound can't do that by POST, then how can I do that? Does anyone have ideas about this? Thanks!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Hanfei Sun
  • 45,281
  • 39
  • 129
  • 237
  • 1
    What are you trying to achieve? HTTPFound simply redirects the browser to a new URL (see http://en.wikipedia.org/wiki/HTTP_302), the new URL is then requested with GET. You may try with "See Other" (http://en.wikipedia.org/wiki/HTTP_303) if original request was a POST, but still it seems to me you're trying to do something strange :) – Code Painters Sep 26 '12 at 08:54
  • I want to POST a request to a URL and then redirect to that URL, just like in a POST form in the page, where you can go to that URL after submitting the form. – Hanfei Sun Sep 27 '12 at 01:34

1 Answers1

6

You can't do this in Pyramid or any other Server-Side Framework or Language.

Your example code isn't showing a form submission, the code is showing a HTTP redirect. It is instructing the Browser to visit another URL, or in other words, telling the browser to resubmit the request.

This Stack Overflow question discusses this same concept, although in ASP not Python - Response.Redirect with POST instead of Get?

If you were to "submit a request" in Pyramid via GET or POST, you would have to use a library like urllib2, requests, or similar. In those instances, the libraries would have the Pyramid server act as the "submitter" of the request.

If you want to have the User/web-broswer submit the request by POST, you would have to do some fancy footwork/trickery to make the browser do that.

Possible ways to accomplish that would include:

  • use JavaScript AJAX form submission, and return an error code or instruction via JSON telling your JavaScript library to resubmit the same form via POST.
  • redirect the user to a GET page which has the form-data filled out (via the GET arguments) , and use javascript to resubmit the form via POST onload

You can't, in any server side language, tell the browser to resubmit a request by POST. Browsers don't work like that.

You should also know that "Most Browsers" will generally interpret any redirect request to be fetched via GET -- even if the original was a POST. That's not to spec - certain redirect codes are supposed to keep a POST a POST - however browsers don't follow all the specs. There is also no HTTP status code or command to purposefully switch from GET to POST .

Community
  • 1
  • 1
Jonathan Vanasco
  • 15,111
  • 10
  • 48
  • 72