Google Web Toolkit (Beta)
Search this site:

# Class RequestBuilder

public class RequestBuilder
extends Object
Builder for constructing Request objects.

By default, this builder is restricted to building HTTP GET and POST requests due to a bug in Safari's implementation of the `XmlHttpRequest` object.

Please see http://bugs.webkit.org/show_bug.cgi?id=3812 for more details.

### Required Module

Modules that use this class should inherit `com.google.gwt.http.HTTP`.

    <!--                                                                        -->
    <!-- Copyright 2007 Google Inc.                                             -->
    <!-- Licensed under the Apache License, Version 2.0 (the "License"); you    -->
    <!-- may not use this file except in compliance with the License. You may   -->
    <!-- may obtain a copy of the License at                                    -->
    <!--                                                                        -->
    <!-- http://www.apache.org/licenses/LICENSE-2.0                             -->
    <!--                                                                        -->
    <!-- Unless required by applicable law or agreed to in writing, software    -->
    <!-- distributed under the License is distributed on an "AS IS" BASIS,      -->
    <!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        -->
    <!-- implied. License for the specific language governing permissions and   -->
    <!-- limitations under the License.                                         -->

    <module>
      <!-- other inherited modules, such as com.google.gwt.user.User -->
      <inherits name="com.google.gwt.http.HTTP"/>
      <!-- additional module settings -->
    </module>

## Nested Classes

RequestBuilder.Method | HTTP request method constants.

## Fields

GET | Specifies that the HTTP GET method should be used.
POST | Specifies that the HTTP POST method should be used.

## Constructors

RequestBuilder(RequestBuilder.Method, String) | Creates a builder using the parameters for configuration.
RequestBuilder(String, String) | Creates a builder using the parameters values for configuration.

## Methods

sendRequest(String, RequestCallback) | Sends an HTTP request based on the current builder configuration.
setHeader(String, String) | Sets a request header with the given name and value.
setPassword(String) | Sets the password to use in the request URL.
setTimeoutMillis(int) | Sets the number of milliseconds to wait for a request to complete.
setUser(String) | Sets the user name that will be used in the request URL.

## Field Detail

### GET

public static final RequestBuilder.Method GET
Specifies that the HTTP GET method should be used.

### POST

public static final RequestBuilder.Method POST
Specifies that the HTTP POST method should be used.

## Constructor Detail

### RequestBuilder

public RequestBuilder(RequestBuilder.Method httpMethod, String url)
Creates a builder using the parameters for configuration.

#### Parameters

httpMethod
  HTTP method to use for the request
url
  URL that has already has already been encoded. Please see URL.encode(String) and URL.encodeComponent(String) for how to do this.

### RequestBuilder

protected RequestBuilder(String httpMethod, String url)
Creates a builder using the parameters values for configuration.

#### Parameters

httpMethod
  HTTP method to use for the request
url
  URL that has already has already been URL encoded. Please see URL.encode(String) and URL.encodeComponent(String) for how to do this.

## Method Detail

### sendRequest

public Request sendRequest(String requestData, RequestCallback callback)
throws RequestException
Sends an HTTP request based on the current builder configuration. If no request headers have been set, the header "Content-Type" will be used with a value of "text/plain; charset=utf-8".

#### Parameters

requestData
  the data to send as part of the request
callback
  the response handler to be notified when the request fails or completes

#### Return Value

a Request object that can be used to track the request

### setHeader

public void setHeader(String header, String value)
Sets a request header with the given name and value. If a header with the specified name has already been set then the new value overwrites the current value.

#### Parameters

header
  the name of the header
value
  the value of the header

### setPassword

public void setPassword(String password)
Sets the password to use in the request URL. This is ignored if there is no user specified.

#### Parameters

password
  password to use in the request URL

### setTimeoutMillis

public void setTimeoutMillis(int timeoutMillis)
Sets the number of milliseconds to wait for a request to complete. Should the request timeout, the RequestCallback.onError(Request, Throwable) method will be called on the callback instance given to the RequestBuilder.sendRequest(String, RequestCallback) method. The callback method will receive an instance of the RequestTimeoutException class as its Throwable argument.

#### Parameters

timeoutMillis
  number of milliseconds to wait before canceling the request, a value of zero disables timeouts

### setUser

public void setUser(String user)
Sets the user name that will be used in the request URL.

#### Parameters

user
  user name to use