Skip to content

Commit 191d87a

Browse files
committed
Add JSONRequestBody helper
Signed-off-by: ZetaTom <[email protected]>
1 parent 605c5df commit 191d87a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.nextcloud.common
2+
3+
import com.google.gson.Gson
4+
import okhttp3.MediaType.Companion.toMediaType
5+
import okhttp3.RequestBody
6+
import okhttp3.RequestBody.Companion.toRequestBody
7+
8+
class JSONRequestBody() {
9+
private val content = mutableMapOf<String, String>()
10+
11+
constructor(key: String, value: String) : this() {
12+
put(key, value)
13+
}
14+
15+
fun put(key: String, value: String) {
16+
content[key] = value
17+
}
18+
19+
fun get(): RequestBody {
20+
val json = Gson().toJson(content)
21+
return json.toRequestBody(JSON_MEDIATYPE)
22+
}
23+
24+
override fun toString(): String {
25+
return content.toString()
26+
}
27+
28+
companion object {
29+
private val JSON_MEDIATYPE = "application/json; charset=utf-8".toMediaType()
30+
}
31+
}

0 commit comments

Comments
 (0)