Making POST requests with parameters in GWT

前端 未结 4 1388
无人及你
无人及你 2021-02-04 05:53

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.



        
相关标签:
4条回答
  • 2021-02-04 06:04

    A web form can't be used to send a request to a page that uses a mix of GET and POST. If you set the form's method to GET, all the parameters are in the query string. If you set the form's method to POST, all the parameters are in the request body.

    Source: HTML 4.01 standard, section 17.13 Form Submission url: http://www.w3.org/TR/html4/interact/forms.html#submit-format

    0 讨论(0)
  • 2021-02-04 06:12

    My recommendation is: Drop the parameter approach.

    Use the @RequestBody instead. It is much cleaner. @RequestParam is only useful if you want to be doing GET request to the server to quickly test rest services. If you are dealing with data of any degree of complexity, you are better of using POST requests to the server that do not have a max content limit.

    Here is an example of how to pump a request to the server. NOTE: in this case, if you are using springboot as the backend, you would have to manipulate the content type fo be application/json.

    private void invokeRestService() {
            try {
                // (a) prepare the JSON request to the server
                RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, JSON_URL);
                // Make content type compatible with expetations from SpringBoot
                // rest web service
                builder.setHeader("Content-Type", "application/json;charset=UTF-8");
    
                // (b) prepare the request object
                UserLoginGwtRpcMessageOverlay jsonRequest = UserLoginGwtRpcMessageOverlay.create();
                jsonRequest.setUserName("John777");
                jsonRequest.setHashedPassword("lalal");
                String jsonRequestStr = JsonUtils.stringify(jsonRequest);
    
                // (c) send an HTTP Json request
                Request request = builder.sendRequest(jsonRequestStr, new RequestCallback() {
    
                    // (i) callback handler when there is an error
                    public void onError(Request request, Throwable exception) {
                        LOGGER.log(Level.SEVERE, "Couldn't retrieve JSON", exception);
                    }
    
                    // (ii) callback result on success
                    public void onResponseReceived(Request request, Response response) {
                        if (200 == response.getStatusCode()) {
                            UserLoginGwtRpcMessageOverlay responseOverlay = JsonUtils
                                    .<UserLoginGwtRpcMessageOverlay>safeEval(response.getText());
                            LOGGER.info("responseOverlay: " + responseOverlay.getUserName());
                        } else {
                            LOGGER.log(Level.SEVERE, "Couldn't retrieve JSON (" + response.getStatusText() + ")");
                        }
                    }
                });
            } catch (RequestException e) {
                LOGGER.log(Level.SEVERE, "Couldn't execute request ", e);
            }
        }
    

    Note the UserLoginGwtRpcMessageOverlay is a patch work. This is not a GwtRpc serializable object, it is a class that extends the gwt javascript object.

    Regards.

    0 讨论(0)
  • 2021-02-04 06:16

    Set the header of your request:

    builder.setHeader("Content-type", "application/x-www-form-urlencoded");
    
    0 讨论(0)
  • 2021-02-04 06:22

    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.

    0 讨论(0)
提交回复
热议问题