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

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

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

  1. HttpClient {
  2. ......
  3. .....
  4. }.plugin(HttpSend).intercept { request ->
  5. val originalCall = execute(request)
  6. val contentType = originalCall.request.content.contentType
  7. val isJson: Boolean = contentType == ContentType("application", "json")
  8. if (isJson) {
  9. //how to find and remove emojis from the body
  10. //then execute(sanitised request)
  11. } else {
  12. originalCall
  13. }

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

llycmphe

llycmphe1#

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

  1. import io.ktor.client.*
  2. import io.ktor.client.engine.cio.*
  3. import io.ktor.client.plugins.api.*
  4. import io.ktor.client.plugins.contentnegotiation.*
  5. import io.ktor.client.request.*
  6. import io.ktor.client.statement.*
  7. import io.ktor.content.*
  8. import io.ktor.http.*
  9. import io.ktor.serialization.kotlinx.json.*
  10. import io.ktor.util.*
  11. import kotlinx.serialization.Serializable
  12. @OptIn(InternalAPI::class)
  13. val plugin = createClientPlugin("no-emojis") {
  14. on(Send) {
  15. val newBody = when (val body = it.body) {
  16. is TextContent -> {
  17. val text = body.text.replace("x", "value") // An example of sanitizing
  18. TextContent(text, body.contentType, body.status)
  19. }
  20. else -> error("I don't know how to sanitize $body")
  21. }
  22. it.body = newBody
  23. proceed(it)
  24. }
  25. }
  26. @Serializable
  27. data class Some(val x: Int)
  28. suspend fun main() {
  29. val client = HttpClient(CIO) {
  30. install(ContentNegotiation) {
  31. json()
  32. }
  33. install(plugin)
  34. }
  35. val r = client.post("https://httpbin.org/post") {
  36. setBody(Some(123))
  37. contentType(ContentType.Application.Json)
  38. }
  39. println(r.bodyAsText())
  40. }
展开查看全部
fdbelqdn

fdbelqdn2#

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

  1. const val REGEX_SANITIZE_EMOJIS: String = "[\\p{C}\\p{So}︀-️\\x{E0100}-\\x{E01EF}]+"
  2. const val REGEX_CLEAN_SPACES: String = " {2,}"
  3. const val PLUGIN_REMOVE_EMOJIS: String = "no more emojis"
  4. val removeEmojiPlugin: ClientPlugin<Unit> = createClientPlugin(PLUGIN_REMOVE_EMOJIS) {
  5. val filterNonUtf8: (String) -> String = { text ->
  6. text
  7. .replace(Regex(REGEX_SANITIZE_EMOJIS), "")
  8. .replace(Regex(REGEX_CLEAN_SPACES), " ")
  9. }
  10. on(Send) {
  11. it.body = when (val body = it.body) {
  12. is TextContent -> {
  13. val text: String = body.text
  14. .replace(Regex(REGEX_SANITIZE_EMOJIS), "")
  15. .replace(Regex(REGEX_CLEAN_SPACES), " ")
  16. TextContent(text, body.contentType, body.status)
  17. }
  18. is FormDataContent -> {
  19. val parameters: Parameters = Parameters.build {
  20. body.formData.forEach { key, values ->
  21. val filteredValues: List<String> = values.map(filterNonUtf8)
  22. this.appendAll(key, filteredValues)
  23. }
  24. }
  25. FormDataContent(parameters)
  26. }
  27. is EmptyContent -> body
  28. else -> body
  29. }
  30. proceed(it)
  31. }
  32. }

在HTTP客户端中安装此插件

  1. HttpClient { install(removeEmojiPlugin) }
展开查看全部

相关问题