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 {
logger.debug("START ActivityPub Request POST url: {}, signer: {}", url, signer?.url)
if (body != null) {
val requestBody = if (body != null) {
val mutableListOf = mutableListOf<String>()
mutableListOf.add("https://www.w3.org/ns/activitystreams")
mutableListOf.addAll(body.context)
body.context = mutableListOf
objectMapper.writeValueAsString(body)
} else {
null
}
val requestBody = objectMapper.writeValueAsString(body)
logger.trace(
"""
@ -123,17 +126,19 @@ class APRequestServiceImpl(
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 u = URL(url)
if (signer?.privateKey == null) {
val bodyAsText = httpClient.post(url) {
header("Accept", ContentType.Application.Activity)
accept(ContentType.Application.Activity)
header("Date", date)
header("Digest", "sha-256=$digest")
setBody(requestBody)
contentType(ContentType.Application.Activity)
if (requestBody != null) {
setBody(requestBody)
contentType(ContentType.Application.Activity)
}
}.bodyAsText()
logBody(bodyAsText, url)
return bodyAsText