Question

I want to access the path users/{id}/permission via a Spray route using the POST method. For some reason (and I've tried different configurations) it doesn't register the route and I get a 405 METHOD NOT ALLOWED.

The code below is part of the pathPrefix "users". GET works, but POST does not.

path(Rest / "permission") { id =>
        /**
         * save permissions object for a user
         */
        post {
          entity(as[String]) { body =>
              try {
                val uperm = parse[UserPermission](body)
                UserPermission.store(uperm)
                respondWithMediaType(`application/json`) {
                  complete {
                    generate(uperm)
                  }
                }
              } catch {
                case e: com.codahale.jerkson.ParsingException =>
                  complete {
                    HttpResponse(BadRequest, "Submitted malformed data.")
                  }
              }
          }
        } ~
          /**
           * grab permissions for a single user
           */
        get {
          try {
            val uperm = UserPermission.fetch(id)
            respondWithMediaType(`application/json`) {
              complete {
                generate(uperm)
              }
            }
          } catch {
            case e: java.lang.NullPointerException =>
              complete {
                HttpResponse(NotFound, "Object not found.")
              }
          }
        }
      } ~

Am I missing something here?

Was it helpful?

Solution

You should't use "Rest" that way, try "PathElement" instead:

path(PathElement / "permission") { id =>
  ...
}

If it's an Int use IntNumber, but Rest is if you want to match the "rest" of the url to the end... so it doesn't make sense the way you used, it should be put at the end.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top