首页 > 解决方案 > Unirest 如何在 Java 中记录请求和响应

问题描述

我刚开始使用 Unirest REST 框架,它很棒,但看不到任何选项来拦截和记录完整的请求和带有标题和正文的响应。有人知道如何实现吗?

标签: log4junirest

解决方案


Unirest.config()
  .intercepter(new LoggingInterceptor());

LoggingInterceptor.java

public class LoggingInterceptor implements kong.unirest.Interceptor {
  
  @Override
  public void onRequest(HttpRequest<?> request, Config config) {
    // Log request
  }
  
  @Override
  public void onResponse(HttpResponse<?> response, HttpRequestSummary request, Config config) {
    // Log response
  }

}

推荐阅读