Fetching Form-data in spring controller - spring-boot

Hi It might look like duplicate but its not.
I am building a rest api using spring boot and need to fetch form-data sent by client app in POST request.
for testing purpose I am using postman. So far i have tried below
#PostMapping("/feed/comment/add/{feedId}")
public ResponseEntity<BaseResponse> addComment(#RequestHeader(name = Constants.USER_ID_HEADER) int userId,
#PathVariable("feedId") int feedId,
#RequestParam("comment") String comment
) {
LOGGER.info("Received add comment request with comment:"+comment);
return new ResponseEntity<BaseResponse>(new BaseResponse("You are not feed owner", RESPONSETYPE.ERROR), HttpStatus.UNAUTHORIZED);
}
this gives error "Required String parameter 'comment' is not present"
Second way tried:
#PostMapping("/feed/comment/add/{feedId}")
public ResponseEntity<BaseResponse> addComment(#RequestHeader(name = Constants.USER_ID_HEADER) int userId,
#PathVariable("feedId") int feedId,
#RequestParam Map<String, String> values
) {
for(String key: values.keySet()) {
System.out.println(key+":"+values.get(key));
}
return new ResponseEntity<BaseResponse>(new BaseResponse("You are not feed owner", RESPONSETYPE.ERROR), HttpStatus.UNAUTHORIZED);
}
this gives wired output:
------WebKitFormBoundarymk97RU1BbJyR0m3F
Content-Disposition: form-data; name:"comment"
test comment
------WebKitFormBoundarymk97RU1BbJyR0m3F--
I'm pretty sure that with plane servlet i can access this using request.getParameter("comment")
not sure how i can fetch it in case of spring rest controller.

"Required String parameter 'comment' is not present" this error happens when this paremeter is required but you didn't send it.
#RequestParam(value="comment", required=false)
this will make the comment parameter optional. So if you missed sending the comment parameter its ok.

Related

request parameters got duplicated when forwarded between two tomcats

We have a Controller running on tomcat 8.5.32 which receives a POST request with query params
/{path_param}/issue?title=4&description=5
request body is empty
Then controller redirects this request to Spring Boot microservice with tomcat 9.0.27.
At line
CloseableHttpResponse result = httpClient.execute(request);
request.getURI().getQuery() equals&title=1&description=2
But when it arrives to microservice parameters are duplicated (title=[4,4]&description=[5,5]).
This is the code which redirects request to microservice
private static <T, U> T executePostRequest(String url, U body, HttpServletRequest httpServletRequest, Function<String, T> readValueFunction) {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
URIBuilder uriBuilder = new URIBuilder(url);
httpServletRequest.getParameterMap().forEach((k, v) -> Arrays.stream(v).forEach(e -> uriBuilder.addParameter(k, e)));
HttpPost request = new HttpPost(uriBuilder.build());
CloseableHttpResponse result = httpClient.execute(request);
String json = EntityUtils.toString(result.getEntity(), "UTF-8");
handleResultStatus(result, json);
return readValueFunction.apply(json);
} catch (IOException | URISyntaxException e) {
...
}
}
I found that there was similar issue with jetty and it was fixed but did not find anything related to tomcat - and how it can be fixed.
I saw also this topic whith suggestion how to handle duplicated parameters in spring boot but i am wondering if anyone else experienced same issue and how did you resolve it if yes.
It's not a bug, it's a feature present in every servlet container.
The Servlet API does not require for the request parameters to have unique names. If you send a POST request for http://example.com/app/issue?title=1&description=2 with a body of:
title=3&description=4
then each parameter will have multiple values: title will have values 1 and 3, while description will have values 2 and 4 in that order:
Data from the query string and the post body are aggregated into the request
parameter set. Query string data is presented before post body data. For example, if
a request is made with a query string of a=hello and a post body of a=goodbye&a=
world, the resulting parameter set would be ordered a=(hello, goodbye, world).
(Servlet specification, section 3.1)
If you want to copy just the first value of the parameters use:
httpServletRequest.getParameterMap()//
.forEach((k, v) -> uriBuilder.addParameter(k, v[0]));

Getting Bad request Error for Ajax call in Spring MVC

I am getting Bad Request Error for my ajax call in Spring MVC. Please refer the code below and kindly comment on this :
ajax call :
$.getJSON('deletRowRequest', {ticketId: ids}, function(data){
alert(data);
});
controller :
#RequestMapping(value="/deletRowRequest", method = RequestMethod.GET)
public #ResponseBody List deleteRow(Model model, #RequestParam(value="ticketId") String ticketId){
String[] ticketString = ticketId.split(",");
String flag = "deleteRow";
List deleteTicketList = new ArrayList();
for(String tick :ticketString){
deleteTicketList.add(tick);
}
System.out.println("list>> "+deleteTicketList);
UpdateDB updatedb = new UpdateDB();
updatedb.updateTable(deleteTicketList, flag);
List ticket = updatedb.getRecordsFromDB();
System.out.println(ticket);
return ticket;
}
I have also included Jackson 1.7.4 jars in the lib folder and <mvc:annotation-driven/> in my servlet.
Please guide me solve this problem.
You're telling String that ticketId is a request param. So spring search for a param in the query string with the name ticketId.
But you're sending ticketId as part of the body of the request.
You have 2 optinos.
Change the invokation, and include your ticketid as part of the querystring.
Change the Spring method, so it could receive the ticketid as part of the body request. You can use #RequestBody instead of #RequestParam.

Empty request body gives 400 error

My Spring controller method looks something like this:
#RequestMapping(method=RequestMethod.PUT, value="/items/{itemname}")
public ResponseEntity<?> updateItem(#PathVariable String itemname, #RequestBody byte[] data) {
// code that saves item
}
This works fine except when a try to put a zero-length item, then I get an HTTP error: 400 Bad Request. In this case my method is never invoked. I was expecting that the method should be invoked with the "data" parameter set to a zero-length array.
Can I make request mapping work even when Content-Length is 0?
I am using Spring framework version 4.1.5.RELEASE.
Setting a new byte[0] will not send any content on the request body. If you set spring MVC logs to TRACE you should see a message saying Required request body content is missing as a root cause of your 400 Bad Request
To support your case you should make your #RequestBody optional
#RequestMapping(method=RequestMethod.PUT, value="/items/{itemname}")
public ResponseEntity<?> updateItem(#PathVariable String itemname, #RequestBody(required = false) byte[] data) {
// code that saves item
}

Send Status code and message in SpringMVC

I have the following code in my web application:
#ExceptionHandler(InstanceNotFoundException.class)
#ResponseStatus(HttpStatus.NO_CONTENT)
public ModelAndView instanceNotFoundException(InstanceNotFoundException e) {
return returnErrorPage(message, e);
}
Is it possible to also append a status message to the response? I need to add some additional semantics for my errors, like in the case of the snippet I posted I would like to append which class was the element of which the instance was not found.
Is this even possible?
EDIT: I tried this:
#ResponseStatus(value=HttpStatus.NO_CONTENT, reason="My message")
But then when I try to get this message in the client, it's not set.
URL u = new URL ( url);
HttpURLConnection huc = (HttpURLConnection) u.openConnection();
huc.setRequestMethod("GET");
HttpURLConnection.setFollowRedirects(true);
huc.connect();
final int code = huc.getResponseCode();
String message = huc.getResponseMessage();
Turns out I needed to activate custom messages on Tomcat using this parameter:
-Dorg.apache.coyote.USE_CUSTOM_STATUS_MSG_IN_HEADER=true
The message can be in the body rather than in header. Similar to a successful method, set the response (text, json, xml..) to be returned, but set the http status to an error value. I have found that to be more useful than the custom message in header. The following example shows the response with a custom header and a message in body. A ModelAndView that take to another page will also be conceptually similar.
#ExceptionHandler(InstanceNotFoundException.class)
public ResponseEntity<String> handle() {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("ACustomHttpHeader", "The custom value");
return new ResponseEntity<String>("the error message", responseHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
}

Handling batched sendgrid events using the asp.net web api

I'm attempting to use an asp.net web api application to handle batched SendGrid events and I've run into a stumbling block due to the way SendGrid handles the content type header of the post it sends.
From their documentation:
Batched event POSTs have a content-type header of application/json,
and contain exactly one JSON string per line, with each line
representing one event. Please note that currently the POST headers
define this post as application/json, though it’s not; each line is a
valid JSON string, but the overall POST body is not.
So, given a controller:
public class SendGridController : ApiController
{
// POST api/values
public void Post([FromBody]string value)
{
// do something with value
}
}
Making a post to it as SendGrid does will result in "value" being null.
string URI = "http://localhost:3018/api/sendgrid/";
string myParameters =
#"={""email"":""foo#bar.com"",""timestamp"":1322000095,""user_id"":""6"",""event"":""bounced""}
{""email"":""foo#bar.com"",""timestamp"":1322000096,""user_id"":""9"",""event"":""bounced""}";
using (var wc = new System.Net.WebClient())
{
wc.Headers[System.Net.HttpRequestHeader.ContentType] = "application/json"; // I work fine if "application/x-www-form-urlencoded" is used.
wc.UploadString(URI, myParameters);
}
If I change the content type in my client example to "application/x-www-form-urlencoded", everything works as expected.
Is there an easy way for me to override this convention such that I can handle the badly formed "json" that sendgrid provides as a string in my controller method?
Ok, I finally figured it out. The trick was to remove the "value" param and work with the request object directly.
So something like:
public class SendGridController : ApiController
{
// POST api/values
public void Post()
{
var value = Request.Content.ReadAsStringAsync().Result;
// do something with value
}
}

Resources