1

I need help. I am trying to use CURL to do HTTP POST & use spray routing along with parameters

curl -v -o UAT.json "http://*****/pvdtest1/14-JUL-2014?enriched=true" -H  "Content-Type: application/json" -d '{ "text": "Test", "username": "User" }'

My JSON Post is optional, means I can also get request as

curl -v -o UAT.json "http://*****/pvdtest1/14-JUL-2014?enriched=true"

In the routing if I use

 path("pvdtest1" / Segment) { (cobDate) =>
          (parameters('enriched.as[Boolean] ? false) & post) {
            (enriched) => {
              println(" inside post")
              entity(as[Message]) { message =>
                println(" inside post 1")
                logger.debug("User '{}' has posted '{}'", message.username, message.text)

above code works file

but if I try to make POST optional, it does not work

 path("pvdtest1" / Segment) { (cobDate) =>
          (parameters('enriched.as[Boolean] ? false) | post) {
            (enriched) => {
              println(" inside post")
              entity(as[Message]) { message =>
                println(" inside post 1")
                logger.debug("User '{}' has posted '{}'", message.username, message.text)



Error:(166, 56) type mismatch;
 found   : spray.routing.Directive0
    (which expands to)  spray.routing.Directive[shapeless.HNil]
 required: spray.routing.Directive[shapeless.HList]
Note: shapeless.HNil <: shapeless.HList, but class Directive is invariant in type L.
You may wish to define L as +L instead. (SLS 4.5)
          (parameters('enriched.as[Boolean] ? false) | post) {

Can someone please help in resolving the issue?

Biffen
  • 6,249
  • 6
  • 28
  • 36
Kunal
  • 11
  • 1

1 Answers1

2

The problem is that the | method in Spray requires that both sides are of the same kind. parameters provides one Boolean, post no value. The & method concats the two directives, so they may may be of different kinds.

You could change that line to the following to allow both get and post, and capture the enriched parameter:

(get | post) { (parameters('enriched.as[Boolean] ? false)

Mark
  • 1,181
  • 6
  • 18
  • 3
    No, `(post & provide(false))` makes no sense (though the rest of the explanation is correct). In spray, `post` and `get` are just filters which you don't have to provide at all. If you want to restrict an inner route to either `get` or `post` requests use `(get | post)`. See my answer at https://groups.google.com/d/msg/spray-user/viCmnOZNi1s/vJTvWn0BSAMJ – jrudolph Oct 29 '14 at 16:42