How to get Grails HeaderParam attributes - java

I came for ruby/rails background. I am facing issues to get header attributes of rest call.
In case of rails, I used to write below code to list all requested header attributes.
puts request.headers.inspect
Could any body please suggest me what is the equivalent for Grails ?

The request object is an instance of the Servlet API's HttpServletRequest interface, so you can use the getHeader and getHeaderNames methods
This is an example of how to print all the headers - add it where you can access the request object (e.g. inside a controller method):
request.getHeaderNames().each {
println(it + ":" + request.getHeader(it))
}

Below is the code, which list all header attributes.
request.headerNames.each{
println it
}
attributes
accept
accept-encoding
content-type
api-key
time-stamp
signature
user-agent
host

Take a look here. On this page you can find "The request object is an instance of the Servlet API's HttpServletRequest interface". You can use getHeader and getHeaderNames methods

Also, remember in Groovy/Grails any getXXX method will treat XXX as a property. So, getHeader and getHeaderNames can be abbreviated to request.header 'someheadername' or request.headerNames

Related

How do I print a request body and headers out of a HTTP Request in Java REST Assured?

While in IntelliJ's debug mode, I found that the value I'm looking for is stored in requestBody and requestHeaders field. However, the problem is that both of these fields are private.
Is there any way to get and print the values of these fields, preferably in Pretty format?
You can use a RequestLoggingFilter and specifiy the LogDetail.HEADERS parameter to log just the request headers.
See here for the filter documentation or the fluent API for logging.
You can use the getReader() method to read the body from an HttpServletRequest object.

Multiple content-types for request in webservice

I have one webservice which can take multiple content-types in request
text/plain
application/json
Now, client can send any of them either json or text.
I have two options available on server
I can create separate apis for different content types
I can parse request data and check if its json or text?
What is better approach here?Is there a design pattern suited for this need?
Note: Management prefer to have one api which can support multiple content-types.
The client must include a Content-Type header indicating the format of the entity they are sending to the server. If the server does not support the format which a client has sent, the expected response is 415 Unsupported Media Type.
I would go with option 1 and have the common logic placed in a seperate method. That way you let the API check and parse the input data for you.
In http you use the "accept"- header to define what type you expect the response to be. The server delivers the content as defined in accept header, the default if it's not set or 406 - "Not acceptable" if the type is not supported
https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
One way would be to use #Path annotations more thoroughly-
The #javax.ws.rs.Path annotation must exist on either the class and/or a resource method. If it exists on both the class and method, the relative path to the resource method is a concatenation of the class and method.
See this link https://docs.jboss.org/resteasy/docs/1.1.GA/userguide/html/Using__Path_and__GET___POST__etc..html

#Produces annotation in JAX-RS

My service method Produces one of this MediaTypes it may produce pdf or excel file or other.
#Produces({"application/pdf","application/vnd.ms-excel"...
My Question
My service returns response type with application/pdf always even if it produces excel. Why?
Than I rearranged MediaTypes.
#Produces({"application/vnd.ms-excel","application/pdf",...
Now it's giving type application/vnd.ms-excel for all responses,again Why?
I am using com.sun.jersey API for client and getting type by the use of
clientResponse.getType()
Probably I think I misunderstood the concept of #Produces annotation.
Please Clarify.
Following is code of my Service method.
response = Response.ok((Object) file);//file is Object of File
response.header("Content-Disposition","attachment; filename="+filename);
//filename can be a.pdf b.xlsx etc
return response.build();
JAX-RS methods should base the preferred content type on the value of the Accept header of your request. Failing that, it should default to the first specified.
While the JAX-RS spec is somewhat vague on the subject, the Jersey documentation is very clear in describing the selection mechanism.
As it said in the documenation:
#GET
#Produces({"application/xml", "application/json"})
public String doGetAsXmlOrJson() {
...
}
The doGetAsXmlOrJson method will get invoked if either of the media types "application/xml" and "application/json" are acceptable. If both are equally acceptable then the former will be chosen because it occurs first.
Also, you can use quality factor for specifying which media type is more preferable:
#Produces({"application/xml; qs=0.9", "application/json"}).
In any way, if you want to be sure about which media type is used, you should divide your methods into two different signatures.
The #Produces annotation is used by the JAX-RS implementation to bind the incoming request to one of your Resource Methods, based on the accept header of the request.
You can set the exact type of the response in the returned Response object using ResponseBuilder#type(MediaType) if you want to enforce one media type in particular.
If you want to match the accept header of the incoming request ("application/vnd.ms-excel" vs "application/pdf" in your case), you can retrieve that header by adding a parameter annotated with #HeaderParam("accept") in your Java method.
HTH.

Routing to specific HTTP method resource by specifying the method in URL

I'm trying to make it possible to use full functionality of my Jersey webservice by both using regular HTTP method calls (GET,PUT,POST,DELETE), and specifying the method in URL while using POST method.
So to delete /resource client will be able to use:
DELETE /resource
or
POST /resource?method=DELETE
Does jersey support that? Or what would be the least-intrusive way to implement that?
The only way I can think of would be writing a Filter which wraps the original HttpServletRequest with my class whose getMethod returns the parsed HTTP method from the URL. Is this the only solution?
Thanks in advance.
Just add PostReplaceFilter to your app: http://jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/container/filter/PostReplaceFilter.html

GWT. Get initial POST parameters

My app started by another app, and at initiating it must get POST parameters. I read that i can get GET parameters using Window.Location.getParameter . But I need to get POST parameter from request. How to implement it?
"get" in getParameter() means "retrieve", it's the standard Java naming convention for getters; it has nothing to do with the HTTP request method.

Categories

Resources