Error when simulating cURL using a REST call in java Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Error when simulating cURL using a REST call in java without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Currently I am able to run the following command in curl which returns the json response as expected. Note that the input JSON is sent by name “json” in the request as a form parameter. The API will accept only JSON requests and will only support POST method calls.


curl --data 'json={ "startdate": "2012-10-10", "enddate":"2012-10-11" }'


{"startdate":"2012-10-10","status":"true","Api Name":"xxxx","output":{"ROW":"49","EU":"54","NA":"33"},"enddate":"2012-10-11"}

I am simulating the above cURL command using a REST call in java.

The code is shown below

String urlString = "";
URL myUrl = new URL(urlString);
URLConnection urlConn = myUrl.openConnection();

String myData = "json={ "startdate": "2012-10-10", "enddate":"2012-10-11" }" ;
urlConn.setRequestProperty("data", myData);


BufferedReader in = new BufferedReader(newInputStreamReader(urlConn.getInputStream()));

String inputLine;

while ((inputLine = in.readLine()) != null) {

But the above code returns the following error.

The stack trace is shown below.

Exception in thread "main" Server returned HTTP response code: 405 for URL:
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at com.intellij.rt.execution.application.AppMain.main(




after openConnection() will fix it? You said your api only allow post requests. And you get a 405 method not allowed.

If not, you can try this link, check out the sendPost() method in first Java HttpURLConnection example chapter:

We are here to answer your question about Error when simulating cURL using a REST call in java - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji