How to send HTTP request GET/POST in Java

In this post, we will see how to send HTTP Get/Post in java.
There are many times when you need to send http get or post request. You can use HttpURLConnection for sending get/post request in java. It belongs to java.net package.

HttpURLConnection example:

We are going to send get or post request to URLs used in restful web services CRUD example, so instead of using PostMan , we will send request using java.

Get Url:

It will give you list of country in json format.

Post URL:

It will create a new country resource and add it to list of counties.
Java code:
When you run above program, you will get below output:
If you comment http.sendingPostRequest(); and send only get request. You will get below output:
You can see USA is added to country list.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *