Last active
September 16, 2025 08:16
-
-
Save dipendra-sharma/565b4f3b760d356cea4c4510ad55b7b4 to your computer and use it in GitHub Desktop.
Android OkHttp Interceptor: Auto-Generate cURL Commands for API Debugging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.apnamart.apnaconsumer.core_app_framework.retrofit | |
| import android.util.Log | |
| import okhttp3.Interceptor | |
| import okhttp3.Response | |
| import okio.Buffer | |
| import java.io.IOException | |
| class CurlLoggingInterceptor(private val tag: String = "AppLogger") : Interceptor { | |
| @Throws(IOException::class) | |
| override fun intercept(chain: Interceptor.Chain): Response { | |
| val request = chain.request() | |
| // Build the curl command | |
| val curlCommand = buildCurlCommand(request) | |
| // Execute the request | |
| val response = chain.proceed(request) | |
| // Read the response body | |
| val responseBody = response.body | |
| val responseBodyString = if (responseBody != null) { | |
| val source = responseBody.source() | |
| source.request(Long.MAX_VALUE) // Buffer the entire body | |
| val buffer = source.buffer | |
| val charset = responseBody.contentType()?.charset(Charsets.UTF_8) ?: Charsets.UTF_8 | |
| buffer.clone().readString(charset) | |
| } else { | |
| "" | |
| } | |
| // Log the formatted output | |
| logCurlAndResponse(request.url.toString(), curlCommand, responseBodyString) | |
| return response | |
| } | |
| private fun buildCurlCommand(request: okhttp3.Request): String { | |
| val builder = StringBuilder() | |
| // Start with curl command and method | |
| builder.append("curl -X ${request.method} \\") | |
| // Add all headers from the request | |
| for (i in 0 until request.headers.size) { | |
| val name = request.headers.name(i) | |
| val value = request.headers.value(i) | |
| builder.append("\n -H \"$name: $value\" \\") | |
| } | |
| // Add request body if present | |
| request.body?.let { body -> | |
| val buffer = Buffer() | |
| body.writeTo(buffer) | |
| val charset = body.contentType()?.charset(Charsets.UTF_8) ?: Charsets.UTF_8 | |
| val bodyString = buffer.readString(charset) | |
| builder.append("\n -d '$bodyString' \\") | |
| } | |
| // Add URL | |
| builder.append("\n \"${request.url}\" \\") | |
| // Add follow redirects flag | |
| builder.append("\n -L") | |
| return builder.toString() | |
| } | |
| private fun logCurlAndResponse(url: String, curlCommand: String, responseBody: String) { | |
| val divider = "────────────────────────────────────────────────────────────────────────────────────────" | |
| val logMessage = buildString { | |
| append("URL: $url\n") | |
| append("$divider\n") | |
| append(curlCommand) | |
| append("\n\n") | |
| if (responseBody.isNotEmpty()) { | |
| append(responseBody) | |
| append("\n") | |
| } | |
| append("$divider\n") | |
| } | |
| Log.d(tag, logMessage) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment