"missing boundary headers" 与 ws.url 在 scala play 中

"missing boundary headers" with ws.url in scala play

我正在使用以下代码针对虚拟端点测试服务:

ws.url(dummyService).withHeaders(HeaderNames.CONTENT_TYPE -> "multipart/form-data; boundary=-----{}}AAA{{}-----").post(myData)

这会生成请求,headers 设置正确。

在我的模拟服务中,我这样处理响应:

def checkData = Action(parse.multipartFormData) { request =>
    request.body.files.find(_.filename.endsWith("testfail.pdf")) match {
      case Some(invalidFile) => BadRequest("Parse Fail")
      case None => Ok("Parse Success")
    }
  }

当我运行测试时,我得到一个错误400,以及以下信息:

For request 'POST /TEST/process' [Missing boundary header]

我做错了什么?

为了使用 Action(parse.multipartFormData),您必须确保相应的 POST 请求使用 multipart/form-data (more on when to use it).[=15 的格式编码=]

换句话说,您需要像这样在模板中定义表单:

@helper.form(action = routes.MyApp.upload, 'enctype -> "multipart/form-data") {
  // ...
}

发送具有不同编码的 POST 会导致 [Missing boundary header] 错误。