Python 2.7 has reached end of supportand will bedeprecatedon January 31, 2026. After deprecation, you won't be able to deploy Python 2.7applications, even if your organization previously used an organization policy tore-enable deployments of legacy runtimes. Your existing Python2.7 applications will continue to run and receive traffic after theirdeprecation date. We recommend thatyoumigrate to the latest supported version of Python.

Issuing HTTP(S) Requests

Region ID

TheREGION_ID is an abbreviated code that Google assignsbased on the region you select when you create your app. The code does notcorrespond to a country or province, even though some region IDs may appearsimilar to commonly used country and province codes. For apps created after February 2020,REGION_ID.r is included in App Engine URLs. For existing apps created before this date, the region ID is optional in the URL.

Learn moreabout region IDs.

This page describes how to issue HTTP(S) requests from your App Engineapp.

This API is supported for first-generation runtimes and can be used whenupgrading to corresponding second-generation runtimes. If you are updating to the App Engine Python 3 runtime, refer to themigration guide to learn about your migration options for legacy bundled services.By default, App Engine uses the URL Fetch service to issue outboundHTTP(S) requests.

For details on request size limits and which headers are sent in a URL Fetchrequest, seeOutbound Requests.

If you haveset up Serverless VPC Accessor if you use theSockets API, youneed tostop URL Fetch from handling requests.URL Fetch causes requests to your VPC network or tothe Sockets API to fail. After you disable URL Fetch, the standard Pythonlibrary will handle HTTP requests. If you need the features provided byURL Fetch for specific requests, you can use theurlfetch library directlyfor those specific requests.

Issue an HTTP request

To issue an outbound HTTP request, use theurlfetch.fetchmethod. For improved code portability, you can also use the Python standardlibrariesurllib,urllib2, orhttplib to issue HTTP requests.When you use these libraries in App Engine, they perform HTTP requests usingApp Engine's URL Fetch service. You can also use the third-partyrequests library as long as you configure it to use URLFetch.

urlfetch

The following snippets demonstrate how to perform a basic HTTPGET request usingurlfetch. First, importtheurlfetch library from the App Engine SDK:

fromgoogle.appengine.apiimporturlfetch

Next, useurlfetch to perform theGETrequest:

url='http://www.google.com/humans.txt'try:result=urlfetch.fetch(url)ifresult.status_code==200:self.response.write(result.content)else:self.response.status_code=result.status_codeexcepturlfetch.Error:logging.exception('Caught exception fetching url')

The following snippet demonstrates how to perform a more advanced request,submitting data from a web form via a HTTPPOST request usingurlfetch:

try:form_data=urllib.urlencode(UrlPostHandler.form_fields)headers={'Content-Type':'application/x-www-form-urlencoded'}result=urlfetch.fetch(url='http://localhost:8080/submit_form',payload=form_data,method=urlfetch.POST,headers=headers)self.response.write(result.content)excepturlfetch.Error:logging.exception('Caught exception fetching url')

urllib2

The following snippets demonstrate how to perform a basic HTTPGET request usingurllib2. First, importtheurllib2 library:

importurllib2

Next, useurllib2 to perform theGETrequest:

url='http://www.google.com/humans.txt'try:result=urllib2.urlopen(url)self.response.write(result.read())excepturllib2.URLError:logging.exception('Caught exception fetching url')

requests

To use requests, you'll need to install bothrequests andrequests-toolbelt using thevendoring instructions.

Once installed, use therequests_toolbelt.adapters.appengine module to configure requests to use URLFetch:

importrequestsimportrequests_toolbelt.adapters.appengine# Use the App Engine Requests adapter. This makes sure that Requests uses# URLFetch.requests_toolbelt.adapters.appengine.monkeypatch()

Once configured, you can use requests normally:

url='http://www.google.com/humans.txt'response=requests.get(url)response.raise_for_status()returnresponse.text

For more information about requests' support for Google App Engine, see the documentation forurllib3.contrib.appengine andrequests_toolbelt.appengine

Set a request timeout

You can adjust the default deadline by using theurlfetch.set_default_fetch_deadline() function. This function stores thenew default deadline on a thread-local variable, so it must be set for eachrequest, for example, in a custom middleware.

Disable redirects

Important: To improve the security of your app, it is recommended that youdisable redirects.

If you are using URL Fetch, the underlying URL Fetch service follows up to fiveredirects by default. These redirects could forward sensitive information, suchas authorization headers, to the redirected destination. If your app does notrequire HTTP redirects, it is recommended that you disable the redirects.

To instruct the URL Fetch service to not follow redirects, set thefetchmethod'sfollow_redirectsparameter toFalse.

Issue an HTTPS request

By default, the underlying URL Fetch service validates the certificateof the host it contacts, and rejects requests if the certificatedoesn't match. You don't need to explicitly secure your request.

Disable host certificate validation

To disable automatic host certificate validation in URL Fetch,issue an HTTPS request, and set thevalidate_certificate parametertoFalse when calling theurlfetch.fetch() method.

Issue an asynchronous request

HTTP(S) requests are synchronous by default. To issue an asynchronousrequest, your application must:

  1. Create a new RPC object usingurlfetch.create_rpc().This object represents your asynchronous call in subsequent methodcalls.
  2. Callurlfetch.make_fetch_call()to make the request. This method takes your RPC object and the requesttarget's URL as parameters.
  3. Call the RPC object'sget_result()method. This method returns the result object if the requestis successful, and raises an exception if an error occurredduring the request.

The following snippets demonstrate how to make a basic asynchronousrequest from a Python application. First, import theurlfetch library from the App Engine SDK:

fromgoogle.appengine.apiimporturlfetch

Next, useurlfetch to make the asynchronous request:

rpc=urlfetch.create_rpc()urlfetch.make_fetch_call(rpc,'http://www.google.com/')# ... do other things ...try:result=rpc.get_result()ifresult.status_code==200:text=result.contentself.response.write(text)else:self.response.status_int=result.status_codeself.response.write('URL returned status code{}'.format(result.status_code))excepturlfetch.DownloadError:self.response.status_int=500self.response.write('Error fetching URL')

Set a request timeout

To set a timeout for your request, set theurlfetch.create_rpc()method'sdeadline parameter when you create your RPC object.

Use a callback function

You can define a callback function for your RPC object. The functionwill be called when your application calls a method on theobject—such aswait(),checksuccess(), orget_result()—that causes the object to waitfor the request to complete.

To use a callback function to handle the result of your fetch call:

  1. Create a helper function to define the scope of thecallback.
  2. Create a handler function to handle the result of your fetch call.
  3. Set your RPC object'scallback attribute to the helper function.

The following snippet demonstrates how to invoke a callback function:

defhandle_result(rpc):result=rpc.get_result()self.response.write(result.content)logging.info('Handling RPC in callback: result{}'.format(result))urls=['http://www.google.com','http://www.github.com','http://www.travis-ci.org']rpcs=[]forurlinurls:rpc=urlfetch.create_rpc()rpc.callback=functools.partial(handle_result,rpc)urlfetch.make_fetch_call(rpc,url)rpcs.append(rpc)# ... do other things ...# Finish all RPCs, and let callbacks process the results.forrpcinrpcs:rpc.wait()logging.info('Done waiting for RPCs')

Issue a request to another App Engine app

When issuing a request to another App Engine app, your App Engine appmust assert its identity by adding the headerX-Appengine-Inbound-Appidto the request.If you instruct the URL Fetch service to not follow redirects, App Enginewill add this header to requests automatically.

SeeDisabling redirects for guidance on disablingredirects.

Note: If you are making requests to another App Engine application,use itsREGION_ID.r.appspot.com domain name rather than a custom domainfor your app.

What's next

Learn about the URL Fetch service, such as the headers that aresent in a URL Fetch request inOutbound Requests.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.