Creating a REST route like /users/new - scala

Is it possible to create a REST endpoint in play like:
/users/new
That would also call a method named new ?
I tried doing this like:
def `new`:= {
...
}
The action worked fine, but the line in my route file was not working because of the new keyword:
GET /users/new #controllers.UserController.new
I like following the rubyonrails convention with URL endpoints.

I am not a expert but try this:
GET /users/new #controllers.UserController.new()
Also your function new() is under UserController class?
new is reserved word in Java at least. I made successfully controller:
GET /user/new controllers.UserController.news()

Related

Using Postman to test REST persistence endpoints

I have been trying to build a secured REST endpoint to save (and return) a person object. So far I have a method as below:
#RequestMapping(value = "/save/", method = RequestMethod.POST)
public Person save(#RequestBody Person person) {
return repository.save(person);
}
I have been trying to use Postman to test this endpoint, but don't seem to be able to structure the URL in the correct way. I should say that I have successfully tested find(Long id) (/find/{id}) and findall.
http://localhost:8080/api/save?person={"id":2,"first":"Brian","last":"Smith"}
First, is this the correct way to structure an endpoint for saving an object, and is the Postman structure correct?
Your method is POST. So you should pass on your payload like this.
Also make sure that you have mentioned your web application context root. If api is your context root, then you are correct. But if it is the case then change it to some meaningful name.

Play 2 reverse routing, get route from controller method

Using the Play 2.2.3 framework, given I have a route like this one in my routes file :
GET /event controllers.Event.events
How can I programatically get the "/event" knowing the method I am trying to reach, in this case 'controllers.Authentication.authenticate' ?
I need it for test purpose, because I would like to have better test waiting not for the route itself but for the controllers method really called. So maybe there is another solution than getting the route and test it this way like I do for now :
//Login with good password and login
val Some(loginResult) = route(FakeRequest(POST, "/login").withFormUrlEncodedBody(
("email", email)
, ("password", password)))
status(loginResult)(10.seconds) mustBe 303
redirectLocation(loginResult).get mustBe "/event"
You construct the reverse route in this way:
[full package name].routes.[controller].[method]
In your example:
controllers.routes.Authentication.authenticate
controllers.routes.Events.events
But say you broke your packages out like controllers.auth and controllers.content, then they would be:
controllers.auth.routes.Authentication.authenticate
controllers.content.routes.Events.events
The reverse routes return a Call, which contains an HTTP method and URI. In your tests you can construct a FakeRequest with a Call:
FakeRequest(controllers.routes.Authentication.authenticate)
And you can also use it to test the redirect URI:
val call: Call = controllers.routes.Events.events
redirectLocation(loginResult) must beSome(call.url)

Zend Framework 2: How can I add in RESTful API a custom http method?

I'm trying to create a custom http method in RESTful API. I was reading the documentation and it is said that you can do it buy adding a simple action in controller and then for example conifg your route with child routes with action => action_name but in the code I have spotted addHttpMethodHandler() method in Zend\Mvc\Controller\AbstractRestfulController.php so in controller construct method I have added:
$add = function () {
return new JsonModel(array(
'id' => 2222,
));
};
$this->addHttpMethodHandler('someAction', $add);
var_dump($this->customHttpMethodsMap);
With the var_dump I can see that this new function is added but I just wonder how can I call it or maybe I'm missing the point.
Regards,
I actually wrote a blog post on this because I had so much trouble too.
The problem is that in addition to calling addHttpMethodHandler within the abstract restful controller, you also need to make sure that the Zend Request class knows that your http method exists.
Here is a link to a better explanation: http://richardbrock1.wordpress.com/2013/03/23/custom-http-methods-in-zf2/

Play Framework - Redirect with params

I am trying to figure out how to do a redirect within a controller action in Play (2.0) using Scala.
The redirect using
Redirect(routes.Application.index)
works just fine.
What I cannot figure out from the docs, API, or Google is how to add parameters to the call.
I am coming from Grails where this could be done easily as follows:
redirect action: "index", params: ["key": "value"]
.
The only way I have found is to call Redirect using a string url and a query string, which seems awkward.
Basically I would like to make use of Redirect(Call) somehow, but I do not how to create the Call object using reverse routing.
Am I missing something/not getting the concept in Play/Scala?
Thanks in Advance!
Ellou'
A route is just a function, so you can pass arguments as usual:
// Redirect to /hello/Bob
def helloBob = Action {
Redirect(routes.Application.hello("Bob"))
}
This snippet comes from http://www.playframework.org/documentation/2.0/ScalaRouting (at the bottom)
You can also avoid creating another function just for this in your controller. In your route config, you can simply add something like this:
GET /google #controllers.Default.redirect(to = "http://google.com")

Scalatra - how do we do an internal redirect / forward of request

I want to call another internal url from my scalatra 'controller'. I can't do a simple redirect, as there's some security settings that mean a user has access only to the first url.
Is there a way to do this?
get("/foo") {
servletContext.getRequestDispatcher("/bar").forward(request, response)
}
The get() method is defined as (similar to POST, et al):
def get(transformers : org.scalatra.RouteTransformer*)(action : => scala.Any) : org.scalatra.Route
Depends on what you mean by internal redirect, I presume you just want to execute another route's action. You have a few options of what you can do. This seems to be working for me:
val canonicalEndpoint = get("/first/route") {
//do things in here
}
Then you could subsequently do:
get("/second/route")( canonicalEndpoint.action )
And I think you would get your desired response.
I like saving the whole Route response of the get() as you may also want to use that with scalatra's url() function in routing.