scala - Akka-http and handling an empty body during POST -
suppose handling post message.
the content-type may application/xml or application/json. can unmarshal xml , json documents fine.
however, handle case post method called empty body (the content-type may still xml or json, document empty string). various logging , analytics purposes. right now, method fails since empty body cannot processed either 1 of unmarshallers.
how write unmarshaller case? or can unmarshaller handle null body?
somewhere in code have following. need more marshallers?
implicit def myunmarshaller(implicit mat: materializer): fromentityunmarshaller[myclass] = unmarshaller.firstof[httpentity, myclass]( /* mynullbodymarshaller, */ // need this? myjsonunmarshaller, myxmlunmarshaller )
or can modify existing one? example, one?
def myjsonunmarshaller(implicit mat: materializer): fromentityunmarshaller[myclass] = unmarshaller.bytestringunmarshaller.forcontenttypes(mediatypes.`application/json`).mapwithcharset { (data, charset) ⇒ val input: string = if (charset == httpcharsets.`utf-8`) data.utf8string else data.decodestring(charset.niocharset.name) val tmp = input.parsejson.convertto[myclass] myclass(tmp.a, tmp.b) }
Comments
Post a Comment