2015-01-29 16 views
9

Ho una route POST Spray e la richiesta contiene un corpo JSON (tipo di contenuto "application/json"). Voglio un modo per estrarre il JSON non elaborato da questa richiesta all'interno del mio percorso.Estrazione di JSON non elaborato come stringa all'interno di una route POST Spray

Per http://host:port/somepath/value1 voglio estratto il corpo posto di TextMsgResponse. Ma per http://host:port/somepath/value2 voglio estratto il corpo dopo solo come JSON grezzo (ad esempio, { "name":"Jack", "age":30 }

val myRoute = path("somepath"/Segment) { pathSegment => 
post { //use only POST requests 
    pathSegment match { 
    case "value1" => 
     entity(as[TextMsgResponse]) { textMsg => 
     complete { 
      //do something with the request 
      StatusCodes.OK 
     } 
     } 
    case "value2" => { 
     //here is I want to extract the RAW JSON from the request   
     } 
    } 
    } 
+1

Hai provato 'entità (come [Array [Byte]]) 'o' entity (as [String]) '? – cmbaxter

+0

Sì ed entrambi non funzionano. –

risposta

8

È possibile utilizzare la direttiva extract come

def rawJson = extract { _.request.entity.asString} 
    . 
    . 
    . 
case "value2" => rawJson{ json =>// use the json 
    } 
Problemi correlati