Paladini
Paladini

Reputation: 4572

How to make a POST request passing JSON as body with Bee Client (Scala)?

I'm using Bee Client as the http-client for my project and I would like to know how can I make a POST request passing a JSON as the body of my request.

I'm using KairosDB as database and I need to query data from http://localhost:8080/api/v1/datapoints/query. KairosDB expects a POST request within the following body:

{
    "start_relative": {
        "value": "5",
        "unit": "years"
    },
    "metrics": [
        { "name": "DP_391366" },
        { "name": "DP_812682" }
    ]
}

How can I achieve a call like that using Bee Client? I've searched for the Bee Client and I didn't find how to pass a JSON string as a body of POST or GET requests, just other arguments (like map).

Please, note that this's a self-answered question.

Upvotes: 1

Views: 2082

Answers (1)

Paladini
Paladini

Reputation: 4572

After search almost the whole Bee Client API and part of it's documentation, I've found this small code example from Bee Client documentation that answers the question (check "Making PUT requests section).

1) Import the following modules:

import uk.co.bigbeeconsultants.http._
import uk.co.bigbeeconsultants.http.request.RequestBody
import uk.co.bigbeeconsultants.http.response.Response
import uk.co.bigbeeconsultants.http.header.MediaType._
import java.net.URL

2) After that, it's simple as that:

// Creating our body within a JSON string.
val jsonBody = RequestBody("""{ "x": 1, "y": true }""", APPLICATION_JSON)

// Making the POST request passing the JSON as body of the request.
val httpClient = new HttpClient
val response: Response = httpClient.post(new URL(query_url), Option(jsonBody))

// Print to check the status of the request.
println(response.status)
println(response.body.asString)

If you want a real case example to better understand how it works, I've made the following code with a little help from PlayJSON:

import uk.co.bigbeeconsultants.http._
import uk.co.bigbeeconsultants.http.request.RequestBody
import uk.co.bigbeeconsultants.http.response.Response
import uk.co.bigbeeconsultants.http.header.MediaType._
import java.net.URL
import play.api.libs.json._

object SimpleApp {
    def main(args: Array[String]) {

        // Creating JSON
        val query_url = "http://localhost:8080/api/v1/datapoints/query"
        val json: JsValue = Json.parse("""
            {
              "start_relative": {
                    "value": "5",
                    "unit": "years"
                },
                "metrics": [
                    {
                        "name": "DP_391366"
                    },
                    {
                        "name": "DP_812682"
                    }
                ]
            }
        """)
        val jsonBody = RequestBody(Json.stringify(json), APPLICATION_JSON)

        // Making API call
        val httpClient = new HttpClient
        val response: Response = httpClient.post(new URL(query_url), Option(jsonBody))

        println(response.status)
        println(response.body.asString)
    }
}

Hope it helps!

Upvotes: 1

Related Questions