Invalid Json: No content to map due to end-of-input when using play body parser

后端 未结 2 1711
孤城傲影
孤城傲影 2021-01-02 09:24

I\'m trying to test a controller method that attempts to parse JSON sent in the request:

  def addRoomToProfileForTime = Action.async(parse.json[AddRoomToPro         


        
相关标签:
2条回答
  • 2021-01-02 09:53

    Another reason can be not setting Content-Length in Postman application. By mistake I had disabled it, and forgot to enable it. .

    0 讨论(0)
  • 2021-01-02 09:56

    The short answer is: on your FakeRequest in your controller test use the withBody method instead of withJsonBody.

    I had this issue as well, and I embarrassingly spent hours on it until I figured it out. The long answer is that FakeRequest's withJsonBody returns a FakeRequest[AnyContentAsJson], and since your controller is expecting a JsValue (not an AnyContentAsJson), when you call apply() on your action it fails to match this apply method, which is the one you want:

    def apply(request: Request[A]): Future[Result]
    

    and instead hits this apply method:

    def apply(rh: RequestHeader): Accumulator[ByteString, Result]
    

    and thus since you're not then passing any Bytes to the Accumulator, you get the unexpected end-of-input error message you're getting.

    0 讨论(0)
提交回复
热议问题