mirror of
https://github.com/corda/corda.git
synced 2025-05-02 16:53:22 +00:00
Merged in clint-httputilsjsonfix (pull request #450)
Fixed HttpApi when sending strings
This commit is contained in:
commit
d195d6f69d
@ -5,10 +5,20 @@ import com.google.common.net.HostAndPort
|
|||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
|
||||||
class HttpApi(val root: URL) {
|
class HttpApi(val root: URL) {
|
||||||
|
/**
|
||||||
|
* Send a PUT with a payload to the path on the API specified.
|
||||||
|
*
|
||||||
|
* @param data String values are assumed to be valid JSON. All other values will be mapped to JSON.
|
||||||
|
*/
|
||||||
fun putJson(path: String, data: Any = Unit) = HttpUtils.putJson(URL(root, path), toJson(data))
|
fun putJson(path: String, data: Any = Unit) = HttpUtils.putJson(URL(root, path), toJson(data))
|
||||||
|
/**
|
||||||
|
* Send a POST with a payload to the path on the API specified.
|
||||||
|
*
|
||||||
|
* @param data String values are assumed to be valid JSON. All other values will be mapped to JSON.
|
||||||
|
*/
|
||||||
fun postJson(path: String, data: Any = Unit) = HttpUtils.postJson(URL(root, path), toJson(data))
|
fun postJson(path: String, data: Any = Unit) = HttpUtils.postJson(URL(root, path), toJson(data))
|
||||||
|
|
||||||
private fun toJson(any: Any) = ObjectMapper().writeValueAsString(any)
|
private fun toJson(any: Any) = if (any is String) any else ObjectMapper().writeValueAsString(any)
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun fromHostAndPort(hostAndPort: HostAndPort, base: String, protocol: String = "http"): HttpApi
|
fun fromHostAndPort(hostAndPort: HostAndPort, base: String, protocol: String = "http"): HttpApi
|
||||||
|
@ -18,12 +18,12 @@ object HttpUtils {
|
|||||||
|
|
||||||
fun putJson(url: URL, data: String) : Boolean {
|
fun putJson(url: URL, data: String) : Boolean {
|
||||||
val body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), data)
|
val body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), data)
|
||||||
return makeRequest(Request.Builder().url(url).put(body).build())
|
return makeRequest(Request.Builder().url(url).header("Content-Type", "application/json").put(body).build())
|
||||||
}
|
}
|
||||||
|
|
||||||
fun postJson(url: URL, data: String) : Boolean {
|
fun postJson(url: URL, data: String) : Boolean {
|
||||||
val body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), data)
|
val body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), data)
|
||||||
return makeRequest(Request.Builder().url(url).post(body).build())
|
return makeRequest(Request.Builder().url(url).header("Content-Type", "application/json").post(body).build())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun makeRequest(request: Request): Boolean {
|
private fun makeRequest(request: Request): Boolean {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user