Charles
Charles

Reputation: 954

Is there a way to get the request body with GET request?

I have this api:

@Path("test")
@GET
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public Parameter performTest(Parameter in) {
    System.out.println(in);
}

but the in always returns null. I can change @GET to @POST and it works but I'm not really performing an create or update so using post seems odd.

Is there a way to get the body with a GET request with jersey?

Upvotes: 0

Views: 170

Answers (1)

Andreas
Andreas

Reputation: 159086

TL;DR The correct solution is to use POST.


"I can change @GET to @POST and it works but I'm not really performing an create or update so using post seems odd"

Why is that odd? POST is not limited to create/update operations.

The specification (RFC 7231, section 4.3.3. POST) says:

The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics. For example, POST is used for the following functions (among others):

  • Providing a block of data, such as the fields entered into an HTML form, to a data-handling process;

  • Posting a message to a bulletin board, newsgroup, mailing list, blog, or similar group of articles;

  • Creating a new resource that has yet to be identified by the origin server; and

  • Appending data to a resource's existing representation(s).

To paraphrase, POST means "here's some data, please process it for me".

Sure, "process" often means "store", as in create/update, but that is not the only way to process data.

In your case, "process" means "run test, using these parameters".

Upvotes: 3

Related Questions