Java: send GET PUT POST to external API and get response
I am relatively new to JAVA and am trying to create a back-end application that will call another server that has data present.
I need to perform GET, POST, PUT and PATCH operations (can work without PATCH) to an external server on the intranet. I can take advantage of a vulnerable service using a postman application (using a custom header), but I want to perform a similar action from my Java code, get the output on my Tomcat Server.
I have limitations in changing dependencies with a POM file or adding banners to a library, so I am looking for a non-Jersey solution.
You can use HttpURLConnection (which extends URLConnection ) to handle HTTP requests, and these classes are part of the JDK itself, so you don't need external jar files, I suggest you look here to learn about it and look here for an example.
Most of the places I'm looking for give me an answer for GET. i see some POSTs too, but my requirement is mostly for PUT.
To place transactions PUT
, DELETE
etc. http, you can use httpConnectionObj.setRequestMethod("PUT")
or httpConnectionObj.setRequestMethod("DELETE")
as mentioned in the example here .
you can set up a simple java.net.Socket instance and implement the calls yourself - then you don't need any dependencies.
http://www.cafeaulait.org/course/week12/22.html
java.net.HttpURLConnection
seems to be the way to go. It can be initialized like this:(HttpURLConnection) new URL(url).openConnection()