Separate GET parameters from POST in embedded Jetty server - java

I need to know wheather the parameter is GET or POST but in handle method request.getParameter(name) gives me all parameters. Is it possible to do something like request.getGETParameter(name) and request.getPOSTParameter(name) or do I have to parse raw data myself?

There is no such thing as GET parameters and POST parameters. GET and POST are methods of HTTP request.
You can find out which method your request is by calling
public String getMethod();
on your request.
You might also want to take a look on the description of HTTP protocol http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
The difference between parameters being sent in GET in and POST method is that in GET request parameters are sent in query string, and in POST request these are sent in request body.

Related

How can we perform 2 get request at same time Rest assured API Testing

JAVA RESTASSURED APITESTNG CUCUMBER
when I send get request using path variable and query param, I get a token in the response body and I will not get proper response until I add that token as a query param and send another get request.
So How can I perform that?
Given Cucumber Feature File
Given API has the following filed <"fieldName">
When API sends a "GET" request to "TranscationAPI"
Then API will receive the response code as 200
And the body will have following field
Should I change it to something like this?
Given API has the following filed <"fieldName">
When API sends a "GET" request to "TranscationAPI"
Then API will receive token as ""
When API sends again "GET" request to "TranscationAPI"
Then API will receive the response code as 200
And the body will have following field
Or I can use handle this through a different method.
Background is the great place to obtain token - see https://cucumber.io/docs/gherkin/reference/#background
It's possible by Java multithreading mechanism - https://www.tutorialspoint.com/java/java_multithreading.htm

Spring MVC Request method 'GET' not supported

I have found few questions on this topic however it does not necessary answer my question
Basicaly
I am passing some values via url so data can be gathered from database. I can do it via method= RequestMethod.GET however I would like to do it via POST so users doesnt see parameters in URL.
I am not sure i am using the best method, i bet there is something much advance in order to achieve this .
Cotroller class
#RequestMapping(value="/empresa", method= RequestMethod.POST)
public String empresa(Model model, Principal principal, #RequestParam("get_Business_ID") String get_Business_ID){
// get selected business
List<Business> selectedBusiness = businessService.getBusinessByBusinessID(get_Business_ID);
System.out.println("business selected= "+ selectedBusiness.get(0).getBusiness_name());
model.addAttribute("selectedBusiness",selectedBusiness);
//Destaque semanal
List<Business> businessList = businessService.getCurrentBusiness();
model.addAttribute("businessList", businessList);
return "empresa";
}
JSP page link
href="${pageContext.request.contextPath}/empresa?get_Business_ID=${business.business_id}"
error Type Status Report
Message Request method 'GET' not supported
Description The method received in the request-line is known by the
origin server but not supported by the target resource.
maybe RequestMethod.GET only works if i am using a form with post method?
Is there any other way to achieve this?
Thanks in advance
You have annotated your method with POST
#RequestMapping(value="/empresa", method= RequestMethod.POST)
So change this to
#RequestMapping(value="/empresa", method= RequestMethod.GET)
If you want it to be a POST request try form submit instead of href
Still you need href? then try this
Make a link use POST instead of GET
You are facing this issue because browser never sends anything, browser only receives requests and forwards them to your back end logic.
Difference between GET is simple fetching of data without any data from your side and POST is in information that you send with your request ( for example you send data that you need to save specific customer, I need this firstName, lastName, email etc to be saved ). With that being said, you can switch between #GetMapping or #PostMapping depending what you need for your application.
To be more precise between get and post
GET A GET method should be used to retrieve data from the server. Multiple get requests to the same URL should be valid and no data should be changed on the server side.
However, this doesn't mean it is not possible to make a GET request change things server side, but you should try to make sure you are following the standard.
POST A POST method should be used when you need to create, update or delete data on the server side. Making the same POST request multiple times may not be safe and may result in inconsistent data. The content of a POST request is sent in the request body. Hence, you don't see the parameters in your browser, but it is easy to see them if you wanted to (Even using the browser developer tools) so it is no more safe than a GET request.

Hiding Parameters in url while sending url through response.sendRedirect method

I am sending url to another server through response.sendRedirect() method and it is generating pdf for me. I am passing all the parameters but one of the parameter data is exceeding length due to which browser is not able to handle it and request is getting blocked.
I know through Post method we can hide url Parameters and response.sendRedirect() uses GET method. Is there any POST method like sendRedirect through which we can access another server url directly through servlet? Thanks in advance.
With response.sendRedirect(newUrl) you send back a HTTP status 302 with a new Location=newUrl in response header. Thus, you cannot force browser to make POST instead of GET method.
What you could do is to consume a pdf file from within your server code and return it to the client, hiding thus from client the actual target location. You can then build any request with method and parameters you want to the new location if it accept it.
See, for example, this tutorial how to make a request to another server from your servlet http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/

Determine type of requested content inside doGet

I have an iframe and a textbox(to enter required url) in my main page, so when user enters required URL I load requested page to iframe.
I am using HttpServlet to handle get requests through doGet. I need to distinguish the requested content type, because if it is a file(img or script) I just read it and return, but if it is an html page, I make some modifications.
I tried to use request.getContentType() inside doGet but it returns null. So is there any way to do this? Thank You
HttpServletRequest#getContentType() returns the value of the Content-Type header if there is one.
You need to specify it when sending the request. You can use Javascript to do this.
Alternatively, but not ideally, you could use a query string parameter to hint at content type.
That's for getting the content type of the request body.
If you want to specify what content the response should have, you need to specify the Accept header with an appropriate media type.
You can alternatively, do URL extension matching. For example, www.host.com/some/path.xml would return XML.
The request.getContentType() method will only return a value if your request body contains data. Since it's a GET it does not contain any body. If you have any data it's either part of the URL or in a query-string attached to the URL. It's pointless to declare Content-type in GET requests, so there is no header to read.
You have to look for the data that you need in your request URL.
If it's a URL generated by a link or image, then get the name and extension from the URL.
It it's generated by a script, it might have that information elsewhere, such as in some variable in the query string (?file-name=xyz&file-type=png for example) or in extra path information (/servlet/xyz/jpeg for example). It depends on how your client is requesting the data.
Do you want to know content type of request, mabye you don't know what for you requests check your URL. If your request contain no data you can request.getContentType should return null value. You can do it on response not request. Mabye you can pass parameter do define what response do you want. I suppouse you have to check response type to determine behaviour of your application depends on the response type not request. Simple GET request only wait for response. And based on that response do some actions.

How to handle POST response using Servlet

I'm new to Java Servlet programming and have a question about how to handle POST response from other servers (not user's POST request) using Servlet programming.
Suppose my application needs to consult another server in order to process user's request. I need to
send an asynchronous POST request (i.e. specify a redirect_uri in the POST request body) to the other server;
handle the POST response from the other server;
present some result to the user.
I think I need one Servlet to handle user's request and send a POST request to the other server, and I need another Servlet (since the POST request is asynchronous) to handle the POST response from the other server. My specific questions are:
What's the best way to send a POST request in this case? For example, using HttpUrlConnection?
How to handle a POST response in a Servlet? It confused me because a servlet is supposed to handle "request" not "response" but in this case the incoming message is indeed a POST response from the other server. In particular, if you can point me the relevant API/method that would be really helpful. For example, in doPost()? How to get the POST response body? (I assume we can get it from HttpServletRequest object).
Thanks very much!
Yue
I did not understand the term 'POST Response'.However may be you are talking about servlet chaining scenario if I understood your requirement correctly.
Servlet Chaining means the output of one servlet act as a input to
another servlet. Servlet Aliasing allows us to invoke more than one
servlet in sequence when the URL is opened with a common servlet
alias. The output from first Servlet is sent as input to other Servlet
and so on. The Output from the last Servlet is sent back to the
browser. The entire process is called Servlet Chaining.
Example of Servlet Chaining

Categories