Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I log RESTful post data?

We have REST services exposed via Spring MVC. We use a HandlerExceptionResolver to log exceptions. We currently log the following:

  • The exception and its stack trace
  • The URL
  • The request headers

It would make debugging easier if we could also log the JSON post data as well. Any suggestions on how to get this?

like image 211
Tom Norton Avatar asked Sep 11 '12 13:09

Tom Norton


People also ask

CAN REST API use POST?

Use an HTTP POST request to send single or multiple RPC requests to the REST API. You can use the POST request to do device configuration.

Can we get data from POST API?

Yes, you can make it work at least using WCF, it's bit different in MVC and Web API where you add attributes to methods like [GET] [POST] etc..

How do I POST a RESTful API?

To post JSON to a REST API endpoint, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header.

Is Login API GET or POST?

Always POST , and preferably with SSL (as in: https://... ). Because the parameters in GET get stored all over the place for caching reasons. So, if you boss needs a reason: security.

Is REST API GET or POST?

GET requests should be used to retrieve data when designing REST APIs; POST requests should be used to create data when designing REST APIs. Creating something is a side effect — if not the point. The HTTP GET method isn't supposed to have side effects. It's considered read-only for retrieving data.


2 Answers

Add this to the class representing the configuration for the application:

import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.filter.AbstractRequestLoggingFilter;

....

@Bean
public Filter loggingFilter(){
    AbstractRequestLoggingFilter f = new AbstractRequestLoggingFilter() {

        @Override
        protected void beforeRequest(HttpServletRequest request, String message) {
            System.out.println("beforeRequest: " +message);
        }

        @Override
        protected void afterRequest(HttpServletRequest request, String message) {
            System.out.println("afterRequest: " +message);
        }
    };
    f.setIncludeClientInfo(true);
    f.setIncludePayload(true);
    f.setIncludeQueryString(true);

    f.setBeforeMessagePrefix("BEFORE REQUEST  [");
    f.setAfterMessagePrefix("AFTER REQUEST    [");
    f.setAfterMessageSuffix("]\n");
    return f;
}

you may have to comment out

   f.setIncludePayload(true);
like image 55
nyxee Avatar answered Oct 12 '22 19:10

nyxee


There is no easy way to log the payload of the request/response. You can use a java web filter to intercept all the requests and responses and read the JSON data from the stream. But there is one problem, when you will read data from the stream the actual data will be exhausted from stream.

Therefore, you have to implement the wrapper of actual request and response object. Only the copied version of request response will be logged. We have implemented similar solution like follows and it satisfied our requirement:

http://www.wetfeetblog.com/servlet-filer-to-log-request-and-response-details-and-payload/431

http://angelborroy.wordpress.com/2009/03/04/dump-request-and-response-using-javaxservletfilter/

like image 41
Shamim Ahmmed Avatar answered Oct 12 '22 19:10

Shamim Ahmmed