Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the request string including parameters

Tags:

java

servlets

I have an odd exception in our application and I'd like to log when it occurs and include the complete request string including the parameters.

When I try

log.warn("Weird request " + request.getRequestURL()); 

I get the request string but not the parameters which were included with ? and &.

example:

/testRequest.do?param1=1&param2=2 

I only see

/testRequest.do 

Can I get this whole string somewhere?

like image 637
sproketboy Avatar asked Oct 16 '09 16:10

sproketboy


People also ask

How do I get parameters in GET request?

GET parameters (also called URL parameters or query strings) are used when a client, such as a browser, requests a particular resource from a web server using the HTTP protocol. These parameters are usually name-value pairs, separated by an equals sign = .

How do I get parameters from request object?

To get all request parameters in java, we get all the request parameter names and store it in an Enumeration object. Our Enumeration object now contains all the parameter names of the request. We then iterate the enumeration and get the value of the request given the parameter name.

Can you pass parameters to get request?

Parameters can be passed in GET Request, if you are not sure how to do a GET Request using Postman, please take a look at the previous article How to make a GET Request. Since now you know how to make a GET request, we will move ahead with sending parameters in a GET request.


1 Answers

See HttpServletRequest#getQueryString()

If you want the whole string, you'll have to append the request url and the query string together as there is no method to get the whole thing.

System.out.println(request.getRequestURL().append('?').append(request.getQueryString())); 
like image 179
Kevin Avatar answered Sep 20 '22 18:09

Kevin