fix: apPost時にbodyがnullのときリクエストボディにnullと出力されてしまう問題を修正

This commit is contained in:
usbharu 2023-10-31 16:27:17 +09:00
parent dbfc4ed1a7
commit 1eabe75cd9
1 changed files with 11 additions and 6 deletions

View File

@ -98,14 +98,17 @@ class APRequestServiceImpl(
override suspend fun <T : Object> apPost(url: String, body: T?, signer: User?): String { override suspend fun <T : Object> apPost(url: String, body: T?, signer: User?): String {
logger.debug("START ActivityPub Request POST url: {}, signer: {}", url, signer?.url) logger.debug("START ActivityPub Request POST url: {}, signer: {}", url, signer?.url)
if (body != null) { val requestBody = if (body != null) {
val mutableListOf = mutableListOf<String>() val mutableListOf = mutableListOf<String>()
mutableListOf.add("https://www.w3.org/ns/activitystreams") mutableListOf.add("https://www.w3.org/ns/activitystreams")
mutableListOf.addAll(body.context) mutableListOf.addAll(body.context)
body.context = mutableListOf body.context = mutableListOf
objectMapper.writeValueAsString(body)
} else {
null
} }
val requestBody = objectMapper.writeValueAsString(body)
logger.trace( logger.trace(
""" """
@ -123,17 +126,19 @@ class APRequestServiceImpl(
val sha256 = MessageDigest.getInstance("SHA-256") val sha256 = MessageDigest.getInstance("SHA-256")
val digest = Base64Util.encode(sha256.digest(requestBody.toByteArray())) val digest = Base64Util.encode(sha256.digest(requestBody.orEmpty().toByteArray()))
val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT"))) val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT")))
val u = URL(url) val u = URL(url)
if (signer?.privateKey == null) { if (signer?.privateKey == null) {
val bodyAsText = httpClient.post(url) { val bodyAsText = httpClient.post(url) {
header("Accept", ContentType.Application.Activity) accept(ContentType.Application.Activity)
header("Date", date) header("Date", date)
header("Digest", "sha-256=$digest") header("Digest", "sha-256=$digest")
if (requestBody != null) {
setBody(requestBody) setBody(requestBody)
contentType(ContentType.Application.Activity) contentType(ContentType.Application.Activity)
}
}.bodyAsText() }.bodyAsText()
logBody(bodyAsText, url) logBody(bodyAsText, url)
return bodyAsText return bodyAsText