GWT HTTP response getText () as binary

I am working on a GWT application that makes a REST request for binary data. I am trying to use the GWT RequestBuilder. The problem is that the answer only offers the getText () method.

Here is a simple example that reproduces the problem:

private static void sendRequest() { String url = URL.encode("/object/object_id"); RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, url); try { requestBuilder.sendRequest("", new RequestCallback() { @Override public void onResponseReceived(Request request, Response response) { String data = response.getText(); ///< Need this to be a byte[] array (eg getData()) } @Override public void onError(Request request, Throwable exception) { } }); } catch (RequestException RequestException) { } } 

The problem is that GWT encodes the response data as String in (as it seems to me) the default platform encoding. Is there a way to get data before GWT converts it to String?

+4
source share
2 answers

HTTP can transmit text and binaries, but Javascript can only receive text through XHR. If you want to send binary data through it, then Base64 encode it. GWT can handle Base64 .

Update: in recent browsers (end of 2013) binary array processing can be achieved using TypedArray . To do this, see browser support .

+3
source

You can get the binary image in GWT using JSNI. Keep in mind that it does not work with IE. This is an example of how:

 native String getBinaryResource(String url) /*-{ // ...implemented with JavaScript var req = new XMLHttpRequest(); req.open("GET", url, false); // The last parameter determines whether the request is asynchronous -> this case is sync. req.overrideMimeType('text/plain; charset=x-user-defined'); req.send(null); if (req.status == 200) { return req.responseText; } else return null }-*/; 

I just finished researching a similar question, where I added additional information: Creating an inline image with gwt java

+1
source

All Articles