Making POST requests with parameters in GWT

Lenz picture Lenz · Oct 21, 2010 · Viewed 20k times · Source

I am trying to do a POST request with a set of parameters to a given URL. The problem I am having is that the POST request is made, but no parameters are passed.

    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);

    StringBuilder sb = new StringBuilder();
    for ( String k: parmsRequest.keySet() ) {
        String vx = URL.encodeComponent( parmsRequest.get(k));
        if ( sb.length() > 0 ) {
            sb.append("&");
        }
        sb.append(k).append("=").append(vx);
    }

    try {
        Request response = builder.sendRequest( sb.toString(), new RequestCallback() {

            public void onError(Request request, Throwable exception) {}

            public void onResponseReceived(Request request, Response response) {}
        });
    } catch (RequestException e) {}
}

This works just fine if I use mode GET and manually add the querystring to the request - but I need to use POST as the data to be passed along may be large....

Answer

z00bs picture z00bs · Oct 21, 2010

Set the header of your request:

builder.setHeader("Content-type", "application/x-www-form-urlencoded");