android 在向服务器请求之前清理/删除所有请求类型中的表情符号

nuypyhwy  于 2023-10-14  发布在  Android
关注(0)|答案(2)|浏览(93)

我需要在发送到服务器之前从请求体(json类型)中删除表情符号,就像我们可以在使用Logger时清理头部一样。到目前为止,我知道我们可以像这样拦截请求

HttpClient {
        ......
        .....
      }.plugin(HttpSend).intercept { request ->
        val originalCall = execute(request)
        val contentType = originalCall.request.content.contentType
        val isJson: Boolean = contentType == ContentType("application", "json")
        if (isJson) {
          //how to find and remove emojis from the body
          //then execute(sanitised request)
        } else {
          originalCall
        }

有没有更好的方法来做到这一点,请分享感谢

llycmphe

llycmphe1#

要清理请求正文,您可以确定HttpRequestBuilder.body属性的类型,并使用清理后的内容创建一个新的请求正文。下面是一个示例:

import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.api.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.content.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.util.*
import kotlinx.serialization.Serializable

@OptIn(InternalAPI::class)
val plugin = createClientPlugin("no-emojis") {
    on(Send) {
        val newBody = when (val body = it.body) {
            is TextContent -> {
                val text = body.text.replace("x", "value") // An example of sanitizing
                TextContent(text, body.contentType, body.status)
            }

            else -> error("I don't know how to sanitize $body")
        }
        it.body = newBody
        proceed(it)
    }
}

@Serializable
data class Some(val x: Int)

suspend fun main() {
    val client = HttpClient(CIO) {
        install(ContentNegotiation) {
            json()
        }
        install(plugin)
    }
    val r = client.post("https://httpbin.org/post") {
        setBody(Some(123))
        contentType(ContentType.Application.Json)
    }
    println(r.bodyAsText())
}
fdbelqdn

fdbelqdn2#

感谢@Aleksei Tirmam给出的解决方案,我设法做到了。

const val REGEX_SANITIZE_EMOJIS: String = "[\\p{C}\\p{So}︀-️\\x{E0100}-\\x{E01EF}]+"
const val REGEX_CLEAN_SPACES: String = " {2,}"
const val PLUGIN_REMOVE_EMOJIS: String = "no more emojis"

val removeEmojiPlugin: ClientPlugin<Unit> = createClientPlugin(PLUGIN_REMOVE_EMOJIS) {

      val filterNonUtf8: (String) -> String = { text ->
        text
          .replace(Regex(REGEX_SANITIZE_EMOJIS), "")
          .replace(Regex(REGEX_CLEAN_SPACES), " ")
      }

      on(Send) {
        it.body = when (val body = it.body) {

          is TextContent -> {
            val text: String = body.text
              .replace(Regex(REGEX_SANITIZE_EMOJIS), "")
              .replace(Regex(REGEX_CLEAN_SPACES), " ")

            TextContent(text, body.contentType, body.status)
          }

          is FormDataContent -> {
            val parameters: Parameters = Parameters.build {
              body.formData.forEach { key, values ->
                val filteredValues: List<String> = values.map(filterNonUtf8)
                this.appendAll(key, filteredValues)
              }
            }

            FormDataContent(parameters)
          }

          is EmptyContent -> body
          else -> body
        }
        proceed(it)
      }
    }

在HTTP客户端中安装此插件

HttpClient { install(removeEmojiPlugin) }

相关问题