Skip to content

Commit

Permalink
fix(plugin-apipost): query parameter parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon406 committed Apr 21, 2022
1 parent c1f8aa4 commit 374b07a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import me.leon.toolsfx.plugin.net.HttpUrlUtil.toParams

fun String.paramsParse() =
split("&").fold(mutableMapOf<String, Any>()) { acc, param ->
println(param)
acc.apply {
if (isNotEmpty()) {
if (param.isNotEmpty()) {
val (key, value) = param.split("=")
acc[key] = value
}
Expand All @@ -32,10 +33,12 @@ fun String.parseCurl() =
("POST".takeIf { acc.method == "GET" } ?: acc.method).also {
val value = s.removeFirstAndEndQuotes(3)
if (value.contains("@file")) {
acc.params.putAll(
value.fromJson(MutableMap::class.java) as
Map<out String, Any>
)
if (value.startsWith("{") || value.startsWith("["))
acc.params.putAll(
value.fromJson(MutableMap::class.java) as
Map<out String, Any>
)
else acc.params.putAll(value.paramsParse().also { println(it) })
} else if (this@parseCurl.contains(
"Content-Type: application/json",
true
Expand Down
20 changes: 18 additions & 2 deletions plugin-apipost/src/test/kotlin/me/leon/plugin/CurlParse.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.leon.plugin

import me.leon.toolsfx.plugin.net.parseCurl
import me.leon.toolsfx.plugin.net.removeFirstAndEndQuotes
import me.leon.toolsfx.plugin.net.*
import org.junit.Test

class CurlParse {
Expand Down Expand Up @@ -64,4 +63,21 @@ class CurlParse {
"'Content-Type: application/json".removeFirstAndEndQuotes().also { println(it) }
"\"Content-Type: application/json".removeFirstAndEndQuotes().also { println(it) }
}

@Test
fun parseFile() {
"apiType=bilibili&token=5c483f653d928ef0c83d3547efb12792&image=@file".paramsParse().also {
println(it)
}
// val raw ="""
// curl https://www.hualigs.cn/api/upload
// -X POST
// -H "accept:application/json, text/javascript, */*; q=0.01"
// -H "user-agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML,
// like Gecko) Chrome/86.0.4240.198 Safari/537.36"
// -d "apiType=bilibili&token=5c483f653d928ef0c83d3547efb12792&image=@file"
// """.trimIndent()
//
// println(raw.parseCurl())
}
}

0 comments on commit 374b07a

Please sign in to comment.