scala - 无法在 Scala HTTP Akka 中测试 POST 请求
问题描述
虽然问题似乎很简单,但我在尝试注册实例时无法获得 OK 200 响应。
def register(InstanceString: String) : server.Route = {
post
{
log.debug(s"POST /register has been called, parameter is: $InstanceString")
try {
val paramInstance : Instance = InstanceString.parseJson.convertTo[Instance](instanceFormat)
handler.handleRegister(paramInstance) match {
case Success(id) =>
complete{id.toString}
}}}
以上是执行请求的方法。
以下是其 API 配置:
paths:
/register:
post:
tags:
- Basic Operations
summary: Register a new instance at the registry
operationId: addInstance
parameters:
- in: body
name: InstanceToRegister
required: true
schema:
$ref: '#/definitions/Instance'
responses:
'200':
description: The ID of the registered instance
schema:
type: integer
format: int64
example: 42
以下是我尝试执行的测试用例,但我得到 400 Bad Request did not equal 200 OK。
"The Server" should {
"Successfully Register" in {
Post("/register", HttpEntity(ContentTypes.`application/json`,"""{"InstanceString":"hallo"}""")) ~> Route.seal(routes) ~> check {
assert(status === StatusCodes.OK)
解决方案
尝试这个:
Post("/register", requestBody.toJson).withHeaders("if Any") ~> Route.seal(routes) ~> check {
status shouldBe StatusCodes.OK
}
我可以看到你Json
的请求正文有阅读器,所以像这样使用 Json writernew InstanceString("hello").toJson
你会得到请求正文。
我看到您的请求内容类型是application/json
并且您正在传递String
。
推荐阅读
- javascript - 如何添加路径来构建节点应用程序 html
- django - Django rest 框架:嵌套序列化器是一种正确的更新方式?
- c++ - 为什么在类构造方法中 std::string 参数在调试时显示不同的结果?
- javascript - How can I execute a __doPostBack function in puppeteer?
- python - 为所有列生成每月均值而不为每列初始化一个列表?
- c - 是否有任何 gcc 编译器选项来强制初始化浮点变量
- .htaccess - 有没有办法在 IIS Windows 托管中允许 .htaccess 文件?
- c# - 什么是 MSSQL,当连接丢失时,在 SP 调用之后?
- angular - Angular HTTP Interceptor subscribing to observable and then returning next.handle but throwing TypeError: You provided 'undefined'
- python - The first and the last date that a variable appear in my dataframe