Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making POST requests with parameters in GWT

Tags:

gwt

smartgwt

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....

like image 916
Lenz Avatar asked Oct 21 '10 07:10

Lenz


2 Answers

Set the header of your request:

builder.setHeader("Content-type", "application/x-www-form-urlencoded");
like image 165
z00bs Avatar answered Oct 20 '22 02:10

z00bs


This should already work - but when using POST, you'll have to read the submitted data differently in your Servlet (I assume, you're using Java on the server side?)

You could try it with a Servlet like this:

public class MyServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                   throws ServletException, IOException {

        System.out.println(req.getReader().readLine());
    }
}

Of course, you can copy the contents of req.getReader() or req.getInputStream() to your own buffer or string etc.

like image 33
Chris Lercher Avatar answered Oct 20 '22 03:10

Chris Lercher