Problem with response.getHeaders()

306 views
Skip to first unread message

Andrei Gheorghe

unread,
Dec 16, 2010, 10:03:14 AM12/16/10
to Google Web Toolkit
Hi there

I am trying to make a REST call through HTTPS and read the response
headers. Even though the response text (response.getText()) has
absolutely no problem, getHeaders() throws a NullPointerException:

java.lang.NullPointerException: null
at com.google.gwt.http.client.Request.getHeaders(Request.java:88)
at com.google.gwt.http.client.Request.access$0(Request.java:86)
at com.google.gwt.http.client.Request$1.getHeaders(Request.java:
53)
at com.bc.adminconsole.client.shared.core.services.ServiceBase
$1.onResponseReceived(ServiceBase.java:76)
at
com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:
287)
at com.google.gwt.http.client.RequestBuilder
$1.onReadyStateChange(RequestBuilder.java:395)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)


My code is mostly like this (RestService creates a RequestBuilder when
calling ()).

RestService restService = new RestService(requestUri, requestMethod,
requestBody, authToken);

RequestCallback r = new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
int responseStatusCode = response.getStatusCode();
if (responseStatusCode != Response.SC_OK && responseStatusCode !=
Response.SC_CREATED) {
// ... stuff ...
} else {
headers = response.getHeaders();
T result = reader.read(response.getText());

resultCallback.onSuccess(result);
}
}

// ... stuff ...
};

restService.setCallback(r);
restService.send();

Any ideas why getHeaders() would return null even though my request
has statusCode 201 and everything else is OK?

Thank you!

marius.andreiana

unread,
Dec 16, 2010, 6:23:42 PM12/16/10
to Google Web Toolkit
Hi Andrei :)

Since nobody answered, I hope the following pointers helps you in
debugging.

A similar thread with a reply from Thomas is here
https://groups.google.com/group/google-web-toolkit/browse_thread/thread/4f4132bc90f6375a

In GWT source, have a look at
* ./user/src/com/google/gwt/http/client/Response.java
* ./user/src/com/google/gwt/http/client/Request.java
String allHeaders = xmlHttp.getAllResponseHeaders();
[line 88] String[] unparsedHeaders = allHeaders.split("\n");


* ./user/src/com/google/gwt/xhr/client/XMLHttpRequest.java
public final native String getAllResponseHeaders() /*-{
return this.getAllResponseHeaders();
}-*/;

See also http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders-method

What does getReadyState() return? (don't know how exactly would this
be called from a Request class). Looks like it works only
if(this.readyState == 2)

M

marius.andreiana

unread,
Dec 16, 2010, 6:35:30 PM12/16/10
to Google Web Toolkit
> What does getReadyState() return? (don't know how exactly would this
> be called from a Request class). Looks like it works only
> if(this.readyState == 2)
And here http://www.devx.com/webdev/Article/33024/1954
it says readyState should be 3 or 4 for response headers to be
returned.
Reply all
Reply to author
Forward
0 new messages