web services - Scala WS exception error (Web page returned instead of Json) -


i using play scala ws send rest api call web server , exception error. json sent server , response server 1 of following.

  1. server returns valid json response.
  2. server returns "no valid json found"
  3. server returns error web page triggers exception error com.fasterxml.jackson.core.jsonparseexception: unexpected character ('<' (code 60)): expected valid value (number, string, array, object, 'true', 'false' or 'null')

how modify code below contents of web page without exception error?

import play.api.libs.ws._  var temptext = helpers.await(ws.url("localhost:9000/someapi").post(jsontosend)).body println(temptext) tempjson = json.parse(temptext) println(tempjson) 

much depends on how "correct" downstream api server is.

in perfect world, assert following facts:

  1. success case => http status 200, http content-type header application/json
  2. "no valid json found" => http status 404 or similar non-200, http content-type header application/json
  3. "error web page" => http status not 200, content-type text/html

if above assertions true, can put little bit of "protection" around our response-handling rather jumping in , trying parse json:

val futureoptionalresult = ws.url("localhost").post("...").map { response =>   response.status match {     case 200 => {       println(response.body)       println(response.json)       some(response.json)     }     case _ => {       println(s"not ok: ${response.status} - body is: ${response.body}")       none     }   } } 

some notes:

  • doing asynchronously easy using await , scales better
  • response.json returns same thing explicit json.parse on body
  • i'm returning option[jsvalue] holds returned json if worked

if above assumptions not true, deeper inspection of content-type header, finer-grained switching on status value and/or other attributes of response needed. luck!


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -