Region ID
The REGION_ID is an abbreviated code that Google assigns based on the region you select when you create your app. The code does not correspond to a country or province, even though some region IDs may appear similar 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 more about region IDs.
This page describes how to issue HTTP(S) requests from your App Engine app.
By default, App Engine uses the URL Fetch service to issue outbound HTTP(S) requests. For details on request size limits and which headers are sent in a URL Fetch request, see Outbound Requests.If you have set up Serverless VPC Access or if you use the Sockets API, you need to stop URL Fetch from handling requests. URL Fetch causes requests to your VPC network or to the Sockets API to fail. After you disable URL Fetch, the standard Python library will handle HTTP requests. If you need the features provided by URL Fetch for specific requests, you can use the urlfetch library directly for those specific requests.
Issue an HTTP request
To issue an outbound HTTP request, use the urlfetch.fetch method. For improved code portability, you can also use the Python standard libraries urllib, urllib2, or httplib to issue HTTP requests. When you use these libraries in App Engine, they perform HTTP requests using App Engine's URL Fetch service. You can also use the third-party requests library as long as you configure it to use URLFetch.
urlfetch
The following snippets demonstrate how to perform a basic HTTP GET request using urlfetch. First, import the urlfetch library from the App Engine SDK:
Next, use urlfetch to perform the GET request:
The following snippet demonstrates how to perform a more advanced request, submitting data from a web form via a HTTP POST request using urlfetch:
urllib2
The following snippets demonstrate how to perform a basic HTTP GET request using urllib2. First, import the urllib2 library:
Next, use urllib2 to perform the GET request:
requests
To use requests, you'll need to install both requests and requests-toolbelt using the vendoring instructions.
Once installed, use the requests_toolbelt.adapters.appengine module to configure requests to use URLFetch:
Once configured, you can use requests normally:
For more information about requests' support for Google App Engine, see the documentation for urllib3.contrib.appengine and requests_toolbelt.appengine
Set a request timeout
You can adjust the default deadline by using the urlfetch.set_default_fetch_deadline() function. This function stores the new default deadline on a thread-local variable, so it must be set for each request, for example, in a custom middleware.
Disable redirects
If you are using URL Fetch, the underlying URL Fetch service follows up to five redirects by default. These redirects could forward sensitive information, such as authorization headers, to the redirected destination. If your app does not require HTTP redirects, it is recommended that you disable the redirects.
To instruct the URL Fetch service to not follow redirects, set the fetch method's follow_redirects parameter to False.
Issue an HTTPS request
By default, the underlying URL Fetch service validates the certificate of the host it contacts, and rejects requests if the certificate doesn'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 the validate_certificate parameter to False when calling the urlfetch.fetch() method.
Issue an asynchronous request
HTTP(S) requests are synchronous by default. To issue an asynchronous request, your application must:
- Create a new RPC object using
urlfetch.create_rpc(). This object represents your asynchronous call in subsequent method calls. - Call
urlfetch.make_fetch_call()to make the request. This method takes your RPC object and the request target's URL as parameters. - Call the RPC object's
get_result()method. This method returns the result object if the request is successful, and raises an exception if an error occurred during the request.
The following snippets demonstrate how to make a basic asynchronous request from a Python application. First, import the urlfetch library from the App Engine SDK:
Next, use urlfetch to make the asynchronous request:
Set a request timeout
To set a timeout for your request, set the urlfetch.create_rpc() method's deadline parameter when you create your RPC object.
Use a callback function
You can define a callback function for your RPC object. The function will be called when your application calls a method on the object—such as wait(), checksuccess(), or get_result()—that causes the object to wait for the request to complete.
To use a callback function to handle the result of your fetch call:
- Create a helper function to define the scope of the callback.
- Create a handler function to handle the result of your fetch call.
- Set your RPC object's
callbackattribute to the helper function.
The following snippet demonstrates how to invoke a callback function:
Issue a request to another App Engine app
When issuing a request to another App Engine app, your App Engine app must assert its identity by adding the header X-Appengine-Inbound-Appid to the request. If you instruct the URL Fetch service to not follow redirects, App Engine will add this header to requests automatically.
See Disabling redirects for guidance on disabling redirects.
What's next
Learn about the URL Fetch service, such as the headers that are sent in a URL Fetch request in Outbound Requests.