Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8229725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:53:51+00:00 2026-06-07T16:53:51+00:00

I will start with: I am doing something terribly wrong. And here is what

  • 0

I will start with: I am doing something terribly wrong. And here is what I am doing wrong.

I created a REST resource for searching something and I am expecting a JSON data in request parameters:

@GET
@Path("/device")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response searchContent(String searchJSONString) {
    String message = new SearchServices().search(searchJSONString);

    return getResponse(message); //Checks the message for any error and sends back the response.
}//end of searchContent()

I should not have written:

@Consumes

since it is a GET resource and it does not consumes anything. But my problem is how to send JSON data in a java code for this (GET resource). I tried curl command which is able to send JSON data to this resource but not a java code by any means.

I tried following curl command to send JSON data to it:

curl -X GET -H "Content-Type: application/json" -d '{"keyword":"hello"}' http://localhost:8080/search-test/rest/search

And its working fine and giving me back a proper JSON response.

But if I am using a curl command without specifying any method (which should be a default http get), I am getting a 405 (Method not allowed) response from tomcat:

curl -d '{"keyword":"hello"}' http://localhost:8080/search-test/rest/search

or through Java code:

HttpURLConnection urlConnection = (HttpURLConnection) new URL(urlString).openConnection();
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestMethod("GET");  //This is not working.

getting the same 405 (Method not allowed) response from tomcat.

If I am sending a GET request using java code, I am not able to send the JSON data as in a post method, and I am forced to use a name=value thing and for that I need to change my REST resource to accept it as a name/value pair.

It means something like this:

http://localhost:8080/search-test/rest/search?param={"keyword":"permission"}

If I am doing something similar in POST:

@POST
@Path("/device")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response searchContent(String searchJSONString) {
    String message = new SearchServices().search(searchJSONString);

    return getResponse(message); //Checks the message for any error and sends back the response.
}//end of searchContent()

I am able to send the JSON data both from Java code and curl command as well:

curl -X POST -H "Content-Type: application/json" -d '{"keyword":"hello"}' http://localhost:8080/search-test/rest/search

or through Java code:

HttpURLConnection urlConnection = (HttpURLConnection) new URL(urlString).openConnection();
urlConnection.setRequestMethod("POST");  //Works fine.
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setDoOutput(true);

Where is the problem? Why am I not able to send it from code but from curl? Is there any other way to send JSON data to the GET resource other than a name=value pair?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:53:53+00:00Added an answer on June 7, 2026 at 4:53 pm

    HttpURLConnection does not allow GET requests with entity and will strip the entity from the request before it is sent to the server. So, I’d strongly recommend avoiding it as even if you use a different Java HTTP client library that allows you to do it, your users will likely run into similar issues (plus web caches and proxies may add more problems).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I will start new project which is based on ASP.NET MVC 3 using C#
I will start new PDA project on the windows mobile and compact framework 2.0
I will start by saying that I don't think what I want can be
As we all know Java program will start executing from the public static void
I use following style attribute so when i will start typing in text box
I wrote an init script to execute last that will start some pythjon script.
I have an asp.net page that calls a dll that will start a long
We have an ASP.Net MVC project that will start with a single web server
I have a Screen name DownloaderScreen when the screen start it will start download
I'm just finishing a relatively big project in scala and will start another related

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.