How to access the body of an HTTP request using RESTEasy

I am looking for a way to directly access the body of an HTTP request. In fact, in my code I get different parameters in the body, and I do not know in advance what exactly I will receive. In addition, I want to be as flexible as possible: I deal with various requests that can change, and I want to process them in one method for each type of request (GET, POST, ...). Is there a way to handle this level of flexibility with RESTEasy? Should I switch to something else?

+6
source share
1 answer

According to the code provided in this answer , you can access the HTTPServletRequest object.

Once you have an HTTPServletRequest object, you should have access to the request body, as usual. One example would be:

String requestBody = ""; StringBuilder stringBuilder = new StringBuilder(); BufferedReader bufferedReader = null; try { InputStream inputStream = request.getInputStream(); if (inputStream != null) { bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); char[] charBuffer = new char[128]; int bytesRead = -1; while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { stringBuilder.append(charBuffer, 0, bytesRead); } } else { stringBuilder.append(""); } } catch (IOException ex) { throw ex; } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException ex) { throw ex; } } } requestBody = stringBuilder.toString(); 
+5
source

Source: https://habr.com/ru/post/926385/


All Articles