Passing a JSON structure to an external URL which is also a Vaadin app

Hi,

Is it possible to send a JSON structure to an external application (also a Vaadin Application) using something the following.

UI.getCurrent().getPage().setLocation("https://a.example.com/my-other-app") 

I know you can build the URL by putting parameters on it but I want to send the JSON as POST data.

Is that possible?

Thanks, John.

Yes, it’s possible. I’d generally recommend going directly from server to server instead of having the browser act as a middle man. See for example this article: https://vaadin.com/learn/tutorials/consuming-rest-services-from-java-applications

I would use a [HttpClient]
(https://www.baeldung.com/httpclient-post-http-request) or [JAX-RS Client]
(https://www.baeldung.com/jersey-jax-rs-client) to send the post request. In the receiving Vaadin Application there should be a REST Controller to receive an handle the post request, for example with [Spring]
(https://www.baeldung.com/rest-with-spring-series).

Thanks both for your suggestions.

Just so that it is clear I think I need to explain what I am trying to do a bit better.

I need to redirect the user’s browser from the server they are on to a new server that runs my vaadin application.
At the moment I am using the following

            String QUERYLINK = "http://host.example.com/app/query?param1=abc?param2=123"
            UI.getCurrent().getPage().executeJavaScript("window.open(\"" + QUERYLINK + "\", \"_self\");");

This works just fine and the user is now on the new server and I have passed in the data and the called application retrieves it.

I want to do exactly the same thing except I don’t want to pass the data params on the URL.
I need to re-direct the users browser to the new application on a different server but I want to pass a JSON structure in the body of the request.

I can use httpClient to create the POST request and I get a response from the Vaadin application but how do I trigger the equivalent of the “window.open” piece so that the user browser re-directed to the new site as well and it is set up to engage with the vaadin application on the new server.

    doItButton.addClickListener(e-> {
            makePostRequest();
            // UI.getCurrent().getPage().executeJs("window.open(\"" + LINK + "\", \"_self\");");
        });

    /**
     * Create the post body. 
     * @return the the post body string
     */
    private String createPostBody() {
        final JsonObject l_payRequest = Json.createObjectBuilder()
                .add("bu", "M1")
                .add("cmt", "ABC")
                .build();
        MLOGGER.debug("Request: " + l_payRequest.toString());
        return l_payRequest.toString();
    }

    /**
     * makePostRequest.
     * @return boolean 200 OK = true else false
     */
    public final boolean makePostRequest() {
        int l_iHTTPResponse = 0;
        try {
            final HttpClient l_httpclient = HttpClientBuilder.create().build();
            final HttpPost l_httppost = new HttpPost("http://wc.sybernet.ie:8075/myapp-1.0.0/app");

            final StringEntity l_postbody = new StringEntity(createPostBody());
            l_postbody.setContentType("application/json");
            l_httppost.setEntity(l_postbody);
            //Execute and get the response.
            final HttpResponse response = l_httpclient.execute(l_httppost);
            MLOGGER.info("HTTP response " + response.getStatusLine().getStatusCode());
            l_iHTTPResponse = response.getStatusLine().getStatusCode();
            if (l_iHTTPResponse == HttpURLConnection.HTTP_OK) {
                final String l_sResponse = EntityUtils.toString(response.getEntity());
                // System.out.println("response: " + l_sResponse);
            } else {
                throw new Exception("makePayment(): Did not get 200 back from executeMethod");
            }
        } catch (Exception e) {
            MLOGGER.error("MakeSSLPostRequest() Exception : " + e.getMessage());
        }
        return (l_iHTTPResponse == HttpURLConnection.HTTP_OK);
    }

Oh thats a very different situation that was originally asked. Using a Client here will not help you. But neither will setting the location of the page, as that will not make a POST request.

I can only think of one way to navigate in a browser with a POST request and that is [submitting a form]
(https://stackoverflow.com/q/1651197/3441504). I think it should be possible to add hidden input fields in the form holding the values you want to post, and make your button the submit button of the form.

I don’t think there is a Form component for Vaadin, but I found [this code example]
(https://vaadin.com/forum/thread/17959073/17968751) that should help you with all aspects of this issue

Kaspar,

Thanks a million for the pointer - that works just perfectly!

Best Regards
John.