2015-01-29 7 views
9

Ich habe eine POST-Spray-Route und die Anfrage enthält einen JSON-Body (Content-Type "application/json"). Ich möchte einen Weg, den rohen JSON aus dieser Anfrage in meiner Route zu extrahieren.Roh-JSON als String in einer Spray-POST-Route extrahieren

Für http://host:port/somepath/value1 möchte ich extrahieren der Postkörper als TextMsgResponse. Aber für http://host:port/somepath/value2 Ich möchte Extrakt der Post Körper nur als Ausgangs Json (zB { "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

Haben Sie versucht, 'Entität (wie [Array [Byte]]) 'oder' Entity (als [String]) '? – cmbaxter

+0

Ja und beide funktionieren nicht. –

Antwort

8

können Sie verwenden, um die extract Richtlinie als

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