-
Notifications
You must be signed in to change notification settings - Fork 596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Directive: Optional entity and empty body #284
Comments
The issue on the mailing list was that entity(as[Option[DeleteUserBody]]) returns a 400 response with this message:
The underlying issue is what to expect for the above entity line. This is statically decided by the choice of the implicit unmarshaller. In this case a JSON unmarshaller was chosen which depends on the body being well-formed JSON (which an empty entity isn't). A JSON unmarshaller for Instead, you could also lift a JSON unmarshaller for /cc @sirthias |
That is indeed a good question. Basically we'd want to deprioritize So, unfortunately, there doesn't appear to be a clean and good solution. The way I see it is that we need to address this via documentation, where we ask people to either |
Would adding a local alias for the implicit val optionsAsEmptyUnmarshaller = Unmarshallers.targetOptionUnmarshaller[HttpRequest, DeleteUserBody] this unmarshaller would have higher priority than any otherwise imported ones? |
I haven't checked but that may provoke an implicit ambiguity if I |
I was going to raise an issue that seems a sub-issue of this one. I define my own JsonSupport in the same vein as SprayJsonSupport is defined. I would like Entity Expected error message instead of
when no body is provided. This also holds for non-optional entity routes. Can this be made a default behavior on the lowest level Unmarshallers, like byteStringUnmarshaller? Judging from MarshallingDirectivesTest in akka-http-tests, the user is expected to handle empty entities by hand, which, to my mind, should be handled automatically |
👍 |
Any plans to fix this issue? |
I ended up doing extractRequest { req =>
val action = (opt: Option[MyEntityType]) => ???
if (req.entity.contentLengthOption.contains(0L)) {
action(None)
} else {
entity(as[MyEntityType]) { e =>
action(Some(e))
}
}
} Is it a reasonable workaround? |
@ymeymann You can enclose it into your own directive: def optionalEntity[T](unmarshaller: FromRequestUnmarshaller[T]): Directive1[Option[T]] =
extractRequest.flatMap { req =>
if (req.entity.contentLengthOption.contains(0L)) {
provide(Option.empty[T])
} else {
entity(unmarshaller).flatMap(e => provide(Some(e)))
}
} |
This should be part of Akka HTTP. |
Actually I've just checked and it doesn't work for empty body. What I've actually used and tested is: def optionalEntity[T](unmarshaller: FromRequestUnmarshaller[T]): Directive1[Option[T]] =
entity(as[String]).flatMap { stringEntity =>
if(stringEntity == null || stringEntity.isEmpty) {
provide(Option.empty[T])
} else {
entity(unmarshaller).flatMap(e => provide(Some(e)))
}
}
} Seems hackish, but I haven't found better way to check whether the body is empty. |
how about just providing an alternate route if you get a rejection from the entity directive: entity(as[SomeModel]) { model =>
complete(OK, "a request body was received")
}
~ complete(OK, "no request body received") |
I tried the solution from @bandrzejczak and it works perfectly for me, I think this should be part of akka http |
I also used the solution from @bandrzejczak after much searching around. |
I do not recommend the solution from @bandrzejczak. When receiving traffic from slower connections it can read an incomplete post body and then attempt to parse it. This can result in intermittent JSON parsing errors. |
@morsecodist Are you sure? Why would it not wait for the entire string to be ready? |
Monday Jun 15, 2015 at 07:17 GMT
Originally opened as akka/akka#17716
As reported in the mailing list akka-http is not able to handle a request with an empty body when route expects an optional entity.
Even an empty string is not a valid json it should still be possible to handle such a request when expected entity is optional.
The text was updated successfully, but these errors were encountered: