scalaplayframework-2.0sprayspray-jsonspray-client

Spray client - treat response with unexpected content-type as application/json?


When I try to GET amazon identity data like that

val pipeline: HttpRequest => Future[IdentityData] = sendReceive ~> unmarshal[IdentityData]
pipeline(Get("http://169.254.169.254/latest/dynamic/instance-identity/document"))

with appropriate case class and formatter, I receive the following exception

UnsupportedContentType(Expected 'application/json')

because amazon mark their response as text/plain content type. They also don't care about the Accept header param. Is there an easy way to tell spray-json to ignore this on unmarshalling?


Solution

  • If you want to extract some IdentityData (which is a case class with a defined jsonFormat) from amazon response, which is a valid json, but with text/plain context type you can simply extract text data, parse it a json and convert into your data, e.g:

    entity.asString.parseJson.convertTo(identityDataJsonFormat)