0

I am using Play Framework's WS API to make requests to a certain service. The service misbehaves if we send integers instead of doubles in JSON POST requests.

Even if we want to send { "value" : 2 }, it would rather accept { "value" : 2.0 }.

Is there a way to to that with Play Framework's JSON API which, I believe, uses json4s.

From what I understands, when sending JSON value, Play Framework sends it as a string with Content-Type : application/json header.

If not a proper way, can there be a workaround for this problem?

Code: (this is what my code would look like with my current approach).

def post(key: String, value: String): Future[(Int, String)] = {
    val body = Json.obj(
      "key" -> key,
      "value" -> number
    )

    WS.url(???).post(body).map(w => (w.status, w.body))
}
chbh
  • 336
  • 2
  • 13
  • 'The service misbehaves' <-- Can you elaborate on this? – planetenkiller Nov 24 '15 at 08:21
  • Can you provide some sample code for how you're constructing the WS call? – Gavin Schulz Nov 24 '15 at 08:45
  • @planetenkiller The problem is that the web service I'm interacting with can only accept one data type for a particular value. Since, the values can vary between integer and doubles, I've defaulted to double so even when an integer is encountered, I send a double. This works fine when I run it with HTTP Client (Postman), but need to do this from inside my Play Framework application. – chbh Nov 24 '15 at 11:52
  • @GavinSchulz I've added some example code. – chbh Nov 24 '15 at 11:54
  • Can't reproduce: `scala> val number: Double = 2 number: Double = 2.0 scala> Json.stringify(Json.obj("value" -> number)) res0: String = {"value":2.0}` – Alvaro Carrasco Nov 24 '15 at 16:43
  • @AlvaroCarrasco Are you sure? Which version of Play Framework or Json4s are you using? I used the same code and it does not work like you mentioned. – chbh Nov 26 '15 at 08:03
  • @chasc Play version 2.3.9 (play doesn't use json4s)... tested it on the REPL... what version are you using? – Alvaro Carrasco Nov 26 '15 at 22:18
  • @AlvaroCarrasco I'm using 2.4.3. I'll try if it works with 2.3.9 – chbh Nov 27 '15 at 10:27
  • @AlvaroCarrasco Just tried it with `2.9.3`. Strange thing, gives me same (2 instead of 2.0) results. – chbh Nov 30 '15 at 09:48
  • @chasc What's the exact code you are using to test it? Can you post the simplest version that exhibits the problem? – Alvaro Carrasco Nov 30 '15 at 16:12
  • @AlvaroCarrasco I have updated the question with the exact code of the application. Also, I tried the exact same code you pasted in the console and doubles were converted to integers upon strinfiifying. – chbh Dec 01 '15 at 11:13

0 Answers0