Skip to content

Commit 8dca91f

Browse files
committed
add logging for sent HTTP headers
The motivation for this was we recently added a new HTTP header, OneSignal-Install-Id, and wanted to confirm it is being sent, however this will help if we need to debug any other headers. This also makes a small improvement to separate out the debug logic from production logic.
1 parent a8943dc commit 8dca91f

File tree

1 file changed

+15
-4
lines changed
  • OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/http/impl

1 file changed

+15
-4
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/http/impl/HttpClient.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import kotlinx.coroutines.withTimeout
2424
import org.json.JSONObject
2525
import java.net.ConnectException
2626
import java.net.HttpURLConnection
27+
import java.net.URL
2728
import java.net.UnknownHostException
2829
import java.util.Scanner
2930
import javax.net.ssl.HttpsURLConnection
@@ -163,16 +164,14 @@ internal class HttpClient(
163164
con.doOutput = true
164165
}
165166

167+
logHTTPSent(con.requestMethod, con.url, jsonBody, con.requestProperties)
168+
166169
if (jsonBody != null) {
167170
val strJsonBody = JSONUtils.toUnescapedEUIDString(jsonBody)
168-
Logging.debug("HttpClient: ${method ?: "GET"} $url - $strJsonBody")
169-
170171
val sendBytes = strJsonBody.toByteArray(charset("UTF-8"))
171172
con.setFixedLengthStreamingMode(sendBytes.size)
172173
val outputStream = con.outputStream
173174
outputStream.write(sendBytes)
174-
} else {
175-
Logging.debug("HttpClient: ${method ?: "GET"} $url")
176175
}
177176

178177
if (cacheKey != null) {
@@ -289,6 +288,18 @@ internal class HttpClient(
289288
}
290289
}
291290

291+
private fun logHTTPSent(
292+
method: String?,
293+
url: URL,
294+
jsonBody: JSONObject?,
295+
headers: Map<String, List<String>>,
296+
) {
297+
val headersStr = headers.entries.joinToString()
298+
val methodStr = method ?: "GET"
299+
val bodyStr = if (jsonBody != null) JSONUtils.toUnescapedEUIDString(jsonBody) else null
300+
Logging.debug("HttpClient: Request Sent = $methodStr $url - Body: $bodyStr - Headers: $headersStr")
301+
}
302+
292303
companion object {
293304
private const val OS_API_VERSION = "1"
294305
private const val OS_ACCEPT_HEADER = "application/vnd.onesignal.v$OS_API_VERSION+json"

0 commit comments

Comments
 (0)