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, applications running in the Java 8 runtime use standard Java classes for HTTP(S) requests, such asjava.net.HttpURLConnection
. You send requests
as you would for any other Java application. To use the default behavior, you
must enable billing for your application or you will get the following
exceptions:
java.net.UnknownHostException
java.net.SocketTimeoutException
java.io.IOException
Using standard runtime network classes
If you use the standard Java network classes, your app will have access to the following features:
- The 32 MB limit on request data is removed.
- Support for HTTP 2.0.
- Supports all Google Cloud-based APIs accessible from the Google Cloud Client Library for Java.
Using URL Fetch
If you have to use URL Fetch in a Java 8 app, add the following line to your appengine-web.xml:
<url-stream-handler>urlfetch</url-stream-handler>
For example:
<xml version="1.0" encoding="utf-8">
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<!-- ... -->
<url-stream-handler>urlfetch</url-stream-handler>
<!-- ... -->
</appengine-web-app>
Issuing an HTTP request
You issue an outbound HTTP request usingjava.net.URLConnection
.
The following snippet demonstrates how to perform a basic HTTP GET
request.
The application creates a new URL
object, then calls the object's
openStream()
method to retrieve the content at that URL:
For more advanced requests, use java.net.HttpURLConnection
as follows:
- Create a new
URL
object. - Create a new
URLConnection
object by calling yourURL
object'sopenConnection()
method. - Create a new
HttpURLConnection
object by casting yourURLConnection
object to theHttpURLConnection
object type. - Set the
HttpURLConnection
object's request method. - Create an output stream for the request.
- Write the request payload to the stream.
- Close the stream.
The following snippet demonstrates how to use HttpURLConnection
to
perform a more advanced request, submitting data from a web form via
a PUT
request:
Setting a request timeout
If you are using URL Fetch, you can adjust the default deadline for
requests using the appengine.api.urlfetch.defaultDeadline
setting in the appengine-web.xml
file.
Setting headers
If you are using URL Fetch, you can set an HTTP header on the outgoing request,
by calling your HttpURLConnection
object's setRequestProperty()
method. The
following snippet sets the X-MyApp-Version
header to 2.7.3
:
conn.setRequestProperty("X-MyApp-Version", "2.7.3");
Disabling redirects
By default,HttpURLConnection
follows HTTP 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 disable this behavior, pass the value false
to your HttpURLConnection
object's setInstanceFollowRedirects()
method:
conn.setInstanceFollowRedirects(false);
If your app uses the underlying urlfetch
package directly
instead of java.net
, your app must specify
doNotFollowRedirects
.
Issuing an HTTPS request
If you are using URL Fetch, issue an HTTPS request using the FetchOptions
class in the urlfetch
package and call validateCertificate()
.
Issuing an asynchronous request
HTTP(S) requests are synchronous by default. To issue an asynchronous
request, your application must use
URLFetchService
's
fetchAsync()
method. This method returns a
java.util.concurrent.Future<HTTPResponse>
.
Issuing 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.