2017-02-08 13 views
0

에 게시 할 때 : 내가 사용하여 서버에 연결하고있어 https://github.com/akka/akka-http/blob/master/docs/src/test/scala/docs/http/scaladsl/SprayJsonExampleSpec.scala#L51"JsArray로 예상 목록 것은"내가 여기 주문/항목 응용 프로그램의 약간의 변화를 만들려고 해요 akka-HTTP 서버

을 httpie, 명령은 다음과 같습니다

HTTP/1.1 400 Bad Request 
Content-Length: 73 
Content-Type: text/plain; charset=UTF-8 
Date: Wed, 08 Feb 2017 19:04:37 GMT 
Server: akka-http/10.0.3 

The request content was malformed: 
Expected List as JsArray, but got "[]" 

코드는 다음과 같습니다 :

나는 다음과 같은 오류가

http POST http://localhost:8080/post_an_order items=[] 

import akka.actor.ActorSystem 
import akka.stream.ActorMaterializer 
import akka.http.scaladsl.Http 
import akka.http.scaladsl.server.Directives._ 
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ 
import spray.json.DefaultJsonProtocol._ 
import scala.io.StdIn 

case class Item(id: Long, name: String) 
case class Order(items: List[Item]) 

object WebServer { 
    implicit val system = ActorSystem() 
    implicit val materializer = ActorMaterializer() 
    implicit val executionContext = system.dispatcher 

    implicit val itemFormat = jsonFormat2(Item) 
    implicit val orderFormat = jsonFormat1(Order) 

    def main(args: Array[String]) { 
    val route = 
     get { 
     pathSingleSlash { 
      complete(Item(123, "DefaultItem")) 
     } 
     } ~ 
     post { 
     path("post_an_order") { 
      entity(as[Order]) { order => 
      val itemsCount = order.items.size 
      val itemNames = order.items.map(_.name).mkString(", ") 
      complete(s"Ordered $itemsCount items: $itemNames") 
      } 
     } 
     } 

    val bindingFuture = Http().bindAndHandle(route, "localhost", 8080) 

    println("http://localhost:8080/") 

    StdIn.readLine() 
    bindingFuture.flatMap(_.unbind()).onComplete(_ => system.terminate()) 
    } 
} 

답변

1

서버가 정상입니다. 당신의 HTTPie 호출로 두 가지 문제 :

  1. JSON 배열은 헤더를 수락
  2. 당신이 HTTPie Accept:application/json을 가정 Akka - HTTP 것 것, 그렇지 않으면 (200)를받을 재정의해야합니다 HTTPie에서 := 할당 연산자를 필요로 406 - Not Acceptable 오류로 다시 방문하십시오.

http POST http://localhost:8080/post_an_order Accept:text/plain items:=[]