Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the HTTP request?

Tags:

rest

jersey

Say normally I have a REST method in Java

@POST      @Path("/test")     @Produces(MediaType.APPLICATION_JSON)     public String showTime(@FormParam("username") String userName) {  : : : } 

which is fine. However, I'm wondering is there a way I can access the full HTTP request with Jersey such as

@POST      @Path("/test")     @Produces(MediaType.APPLICATION_JSON)     public String showTime(@FormParam("username") String userName,@XXXXXX String httpRequest) {  : : : } 

where some annotation would give me the full HTTP request to store in a variable. I have tried using @POST but it doesn't seem to work. Any suggestions?

like image 607
John Avatar asked Feb 25 '11 15:02

John


People also ask

How do I find my HTTP requests?

To view the request or response HTTP headers in Google Chrome, take the following steps : In Chrome, visit a URL, right click , select Inspect to open the developer tools. Select Network tab. Reload the page, select any HTTP request on the left panel, and the HTTP headers will be displayed on the right panel.

What is HTTP request URL?

An HTTP request is made by a client, to a named host, which is located on a server. The aim of the request is to access a resource on the server. To make the request, the client uses components of a URL (Uniform Resource Locator), which includes the information needed to access the resource.

What does it mean HTTP request?

An HTTP request is an action to be performed on a resource identified by a given Request-URL. Request methods are case-sensitive, and should always be noted in upper case. There are various HTTP request methods, but each one is assigned a specific purpose.


2 Answers

You can use the @Context annotation:

@POST  @Path("/test") @Produces(MediaType.APPLICATION_JSON) public String showTime(     @FormParam("username") String userName,     @Context HttpServletRequest httpRequest ) {     // The method body } 
like image 132
sdorra Avatar answered Sep 22 '22 12:09

sdorra


If you want to get the request body, you could use the tip lined out in this post: How to get full REST request body using Jersey?

If you need to know more about the request itself, you could try the @Context annotation as mentioned by sdorra.

like image 22
Jan Thomä Avatar answered Sep 21 '22 12:09

Jan Thomä