Hanfei Sun
Hanfei Sun

Reputation: 47071

How to submit a request by POST in Pyramid?

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!

Upvotes: 1

Views: 1890

Answers (1)

Jonathan Vanasco
Jonathan Vanasco

Reputation: 15690

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 .

Upvotes: 6

Related Questions