Reputation: 1078
Having some troubles communicating with a Express REST api through Java.
A simple route which is online at: http://localhost:5555/test
router.post('/test', function (req, res, next) {
console.log("recived request");
res.sendStatus(200);
});
As you can see, this route doesn't do much tho, only for connection testing purpose.
Spending like hours searching but didn't find a good example yet.
Still got this peace of code but got exception.
URL url = new URL("http://localhost:5555/test");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write("test");
If someone knows a peace of code I could work, that would be great!
Thanks
Edit Server is running:
> node index.js
server running on port: 5555
connection open
Exception from Java:
System.err: null
Upvotes: 0
Views: 598
Reputation: 2717
Because your route accepts POST
method, you may have 2 options.
Change your request method in Java code to POST
connection.setRequestMethod("POST");
Change your route accepts GET
Upvotes: 0
Reputation: 585
Your Express route is expecting a POST
and your code does a GET
request (openConnection
). Try to change it to get
an retry your operation.
Upvotes: 1