我需要使用Spray发布帖子请求,但我不明白如何在服务器端捕获请求。这是我的代码:
客户端:
.
.
val pipeline: HttpRequest => Future[HttpResponse] = sendReceive
val fileName = "document.docx"
val path = getClass.getResourceAsStream("/test-documents/" + fileName)
val bytes = IOUtils.toByteArray(path)
val bytes64 = Base64.encodeBase64(bytes)
val streamInString = new String(bytes64)
val json: JsonInputStream = JsonInputStream(fileName, streamInString)
val jsonString = write(json)
val response: Future[HttpResponse] = pipeline(Post("http://localhost:8080/jsontest/", jsonString))
SERVER(4个测试中没有捕获请求):
startServer(interface = "localhost", port = 8080) {
path("jsontest") {
post {
complete {
<h1>TEST 1</h1>
}
}
} ~
path("jsontest" / Segment) { json =>
post {
complete {
<h1>TEST 2</h1>
}
}
} ~
path("jsontest") {
get {
complete {
<h1>TEST 3</h1>
}
}
} ~
path("jsontest" / Segment) { json =>
get {
complete {
<h1>TEST 4</h1>
}
}
}
}
请帮助我
答案 0 :(得分:0)
-.-只需删除"http://localhost:8080/jsontest/"
和
path("jsontest") {
post {
complete {
<h1>TEST 1</h1>
}
}
}
捕获它。 对不起我的愚蠢
答案 1 :(得分:0)
使用entity
指令
查看http://spray.io/documentation/1.2-RC2/spray-routing/marshalling-directives/entity/#entity
将application/json
用于内容类型