Hi,

I have a requirement to listen to a POST event from server and display the
payload information in UI.

We have Jetty server which runs on port 8080, which will publish a JSON
object to a URL, Example URL: http://localhost:8080/myRest/myRestMethod

Below is the piece of server code which sends the POST request to the above
URL.

// CODE START

private void sendHTTPData(JSONObject json) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
                URLConnection connection = null;
URL url = new URL("http://localhost:8080/myRest/myRestMethod";);
            connection = url.openConnection();
            connection.setDoInput(true);
            ((HttpURLConnection)connection).setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", APPLICATION_JSON);
            connection.setRequestProperty("Accept", APPLICATION_JSON);
            OutputStreamWriter streamWriter = new
OutputStreamWriter(connection.getOutputStream());
            streamWriter.write(json.toString());
            streamWriter.flush();

            //Verify Response
            if (((HttpURLConnection)connection).getResponseCode() ==
HttpURLConnection.HTTP_OK){
                InputStreamReader streamReader = new
InputStreamReader(connection.getInputStream());
                BufferedReader bufferedReader = new
BufferedReader(streamReader);
                String response = null;
                while ((response = bufferedReader.readLine()) != null) {
                    stringBuilder.append(response + "\n");
                }
                bufferedReader.close();
                ((HttpURLConnection)connection).getResponseMessage();
                System.out.println("Received Response..... " +
stringBuilder.toString());
            }
        } catch (Exception e){
            System.out.println(e.getMessage());
        } finally {
            System.out.println("Disconnecting..... HttpURLConnection");
            ((HttpURLConnection)connection).disconnect();
        }
    }

// CODE ENDs

W
​hen server sent the post request, I need to catch it in UI. So how I can
listen to POST request from server?

​My UI runs in node js on port 4200 and I have a proxy connection with
Jetty server as per proxy.config.json set as below
​
{
    "/sample-rest/*": {
      "target": "http://FGT009936567:8080";,
      "secure": false,
      "logLevel": "debug"
    },
    "/myRest/*": {
      "target": "http://FGT009936567:8080";,
      "secure": false,
      "logLevel": "debug"
    }
  }

​Jetty server is running on port 8080 on FGT009936567 machine.​​

--
*Saju Thankathurai,*
*+91-9445127047*

-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to