{"id":2154,"date":"2015-11-27T22:34:08","date_gmt":"2015-11-27T20:34:08","guid":{"rendered":"http:\/\/hasselba.ch\/blog\/?p=2154"},"modified":"2015-11-27T22:34:08","modified_gmt":"2015-11-27T20:34:08","slug":"debug-retrofit-rest-applications","status":"publish","type":"post","link":"https:\/\/hasselba.ch\/blog\/?p=2154","title":{"rendered":"Debug Retrofit REST applications"},"content":{"rendered":"<p>Today I had some problems with designing a <a href=\"http:\/\/square.github.io\/retrofit\/\" target=\"_blank\">Retrofit<\/a> REST application, so I needed a way to debug the request and the response from the server. After poking around, I found the <em>HttpLoggingInterceptor<\/em> from <a href=\"http:\/\/square.github.io\/okhttp\/\" target=\"_blank\">OkHttp<\/a>.\u00a0 It provides all the functionality I need and is really easy to implement.<\/p>\n<p>First you have create a <em>OkHttpClient<\/em> instance, add the interceptor and set the debug level to <em>HttpLoggingInterceptor.Level.BODY:<\/em><\/p>\n<pre><code>OkHttpClient client = new OkHttpClient();\r\nHttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();\r\ninterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);\r\nclient.interceptors().add(interceptor);\u00a0\u00a0\u00a0 \r\n<\/code><\/pre>\n<p>Next step is to add the client during the building of the Retrofit instance:<\/p>\n<pre><code>Retrofit retrofit = new Retrofit.Builder().client(client).build();<\/code><\/pre>\n<p>Now you can see the HTTP data sent over the wire:<\/p>\n<p><a href=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/11\/2015-11-27-21_11_081.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2160\" src=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/11\/2015-11-27-21_11_081.png\" alt=\"2015-11-27 21_11_08\" width=\"1198\" height=\"904\" srcset=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/11\/2015-11-27-21_11_081.png 1198w, https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/11\/2015-11-27-21_11_081-300x226.png 300w, https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/11\/2015-11-27-21_11_081-1024x773.png 1024w\" sizes=\"auto, (max-width: 1198px) 100vw, 1198px\" \/><\/a><\/p>\n<p>Don&#8217;t forget to add the two required dependencies to your <em>pom.xml<\/em>:<\/p>\n<pre><code>&lt;dependency&gt;\r\n   &lt;groupId&gt;com.squareup.okhttp&lt;\/groupId&gt;\r\n   &lt;artifactId&gt;okhttp&lt;\/artifactId&gt;\r\n   &lt;version&gt;2.6.0&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n&lt;dependency&gt;\r\n   &lt;groupId&gt;com.squareup.okhttp&lt;\/groupId&gt;\r\n   &lt;artifactId&gt;logging-interceptor&lt;\/artifactId&gt;\r\n   &lt;version&gt;2.6.0&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Today I had some problems with designing a Retrofit REST application, so I needed a way to debug the request and the response from the server. After poking around, I found the HttpLoggingInterceptor from OkHttp.\u00a0 It provides all the functionality &hellip; <a href=\"https:\/\/hasselba.ch\/blog\/?p=2154\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[89,61,81],"tags":[],"class_list":["post-2154","post","type-post","status-publish","format-standard","hentry","category-java","category-rest","category-web"],"_links":{"self":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2154"}],"version-history":[{"count":5,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2154\/revisions"}],"predecessor-version":[{"id":2164,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2154\/revisions\/2164"}],"wp:attachment":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}