Carl
Carl

Reputation: 2934

How to post a form from GAE?

I am trying to POST a form to a 3rd-party web server.

The form is like this:

<form method=post action="http://www.domain.com/path/page.do?id=18&localeCode=en-us">
<input type="submit" name="FILTER:name=init" value="Submit" />
</form>

If I load this HTML into a browser and submit the form, the correct page is returned by the web server. If I submit it from Swift using Alamofire all is well.

But on App Engine, using urllib2 or urlfetch a different HTML page is returned.

from google.appengine.api import urlfetch
page = urlfetch.fetch(
    url="http://www.domain.com/path/page.do?id=18&localeCode=en-us",
    payload={"FILTER:name=init" : "Submit"},
    method=urlfetch.POST,
    headers={"Content-Type": "application/x-www-form-urlencoded"},
    deadline=30)

For the payload I've tried encoding name=init as name%3Dinit but without any further success.

Upvotes: 1

Views: 463

Answers (1)

Josh J
Josh J

Reputation: 6893

You need to urlencode your payload dict manually before passing to urlfetch.fetch. See the example from the docs below.

import urllib

from google.appengine.api import urlfetch

form_fields = {
  "first_name": "Albert",
  "last_name": "Johnson",
  "email_address": "[email protected]"
}
form_data = urllib.urlencode(form_fields)
result = urlfetch.fetch(url=url,
    payload=form_data,
    method=urlfetch.POST,
    headers={'Content-Type': 'application/x-www-form-urlencoded'})

Upvotes: 5

Related Questions