Skip to page content Skip to main navigation
English | Site Directory

e.g. "templates" or "datastore"

# Overview

App Engine applications can communicate with other applications or access other resources on the web by fetching URLs. This is useful for communicating with web services or retrieving RSS feed data.

The urlfetch.fetch() function performs an HTTP request.

    from google.appengine.api import urlfetch

    url = "http://www.google.com/"
    result = urlfetch.fetch(url)
    if result.status_code == 200:
      doSomethingWithResult(result.content)

fetch() supports five HTTP methods: `GET`, `POST`, `HEAD`, `PUT` and `DELETE`. The request can include HTTP headers, and body content for a `POST` or `PUT` request. For example, to submit data to a web form handler using the `POST` action:

    import urllib

    form_fields = {
      "first_name": "Albert",
      "last_name": "Johnson",
      "email_address": "Albert.Johnson@example.com"
    }
    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'})

### Things To Know About urlfetch

URL fetches are restricted to accessing ports 80 (`http`) and port 443 (`https`). No other ports or URL schemes are supported.

A URL fetch action is synchronous: The fetch() function does not return until the remote server has responded.

Since your application must respond to the user's request within several seconds, a URL fetch action to a slow remote server may cause your application to return a server error to the user. There is currently no way to specify a time limit to the URL fetch action. One way to control the user experience of a slow remote server is to use browser JavaScript to call a separate handler on your application that performs the action that requires URL fetching, then report the error to the user if the handler fails.

App Engine uses a HTTP/1.1 compliant proxy to fetch the result. The proxy can do HTTPS requests, however it cannot authenticate the host it is contacting. All SSL certificates are accepted by the proxy, including self-signed certificates.

fetch() follows HTTP redirects up to 5 times, and returns the final resource. You can tell it not to follow redirects with an argument.

A request handler cannot fetch the URL of its own request. This prevents accidental infinite loops, where the app calls itself forever.

When sending an HTTP POST request, if a `Content-Type` header is not set explicitly, the header is set to `x-www-form-urlencoded`. This is the content type used by web forms.