0

I need make my method capable of accepting JSON data. Say I need to make a GET REST method call with JSON data.

GET /player/login/ HTTP/1.0 

Content-Type: application/json
Request Body    
{
                "username": ”xyz”,
                "password": "234fsf34"
}

I am not getting how to take this JSON data within my REST API method.

@GET
@Path("player/login")
@Produces("application/json)
public responseData loginPlayer(){
}
2
  • The first code fragment looks like an HTTP GET 'request' with a 'Request Body'. The second code fragment looks like some sort of 'handler' which responds to requests. Could you edit to question and clarify a bit, where the problem is? What are you implementing? Server or Client? What is the problem? Commented Jan 18, 2012 at 9:45
  • first part is request url and request body. and second part is the method with would handle that request. am implementing server side, using restapi. Commented Jan 18, 2012 at 10:01

2 Answers 2

1

If you want to use authentication with a GET, the 'proper' way to do it is to use basic access authentication.

http://en.wikipedia.org/wiki/Basic_access_authentication

The client takes the username and password and forms a string of the form user:password. So in your example, that will be:

"xyz:234fsf34"

The client then base64 encodes this string. If the client is also Java, you could use the apache commons Base64 class for encoding/decoding:

http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html

So you get something like:

"eHl6OjIzNGZzZjM0"

And the client sends this in the GET, but as an HTTP Header (not Request Body):

GET /player/login/ HTTP/1.0
  Content-Type: application/json
  Authorization: Basic eHl6OjIzNGZzZjM0

The server reads this in the HTTP Header 'Authorization' much like the example code here: https://cwiki.apache.org/WINK/jax-rs-http-headers.html

Then decodes it with the (Base64 apache commons class).

Then you can respond to the GET with appropriate data.

Sign up to request clarification or add additional context in comments.

Comments

1

From the syntax, it appears you are using JAX-RS.

You need to use the @Consumes annotation.

The documentation is for @Consumes is at: http://docs.oracle.com/javaee/6/tutorial/doc/gilik.html#gipyt

A rather detailed tutorial (which uses Jersey, the reference JAX-RS implementation) is here: http://blogs.oracle.com/enterprisetechtips/entry/configuring_json_for_restful_web

Eventually, your method should be defined to accept the deserialized java object corresponding to the JSON body received from the client, and you need to set up things so that this deserialization is done before your method gets invoked. Read the second link for how to do it.

On a side note: GET methods typically do not have a 'Request Body'. It is kind of weird to have that. The POST method is meant for posting request bodies. While the HTTP standard does not explicitly disallow GET requests to have a body, POST is the appropriate choice.

4 Comments

ArjuneShankar: am able to get data with in method using POST ,but i need to do it only GET only with request body. but even with consume am not getting data with in method. and all examples i found uses POST. please tell me how to use it with GET.
@Romi - like I said, it is weird to 'post' something to an HTTP server within a GET method. This is what a POST method is there for. I wouldn't be surprised if the implementation of JAX-RS simply cannot work with what you are doing. I also wouldn't be surprised if some HTTP client implementations simply cannot send a body in a GET. Why do such a weird thing when you can do it right with a POST?
Actually am not posting data...i juss need to read that data and base on this information, want to fetch some data. basicaly this is for login, and am passing loginId and password with GET.
@Romi - You can do the exact same thing with a POST as well. Post the login information to server, the server reads it, and then sends the data back to client in a response. i.e. An @POST method can have an @Consumes and an @Produces annotation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.