Scalatra Flashcards
Name the 5 CRUD HTTP Methods
POST PUT PATCH GET DELETE
When a user enters an address in the browsers location bar a ____ request is generated
Get
When should you use a GET
- When you are implementing a read-only operation such as the R in CRUD
- When the request can be submitted repeatedly.
- When the response should be bookmarkable or indexed in search engines.
The default method of a web form is ______ but __________________
POST
POST is not limited to web forms.
The request body may contain any arbitrary data, described by the Content-Type header of the request. Other common POST bodies include
JSON, XML and Images
Use POST in the following cases
- When implementing create operations, such as the C in CRUD
- When the server is responsible for generating a URI for the created entity
- When you’re implementing a write operation and nothing else seems to fit.
POST is a good default choice when writing because it is the only CRUD method that is not idempotent. What does this mean
A client should be able to resubmit a GET, DELETE or PUT request and expect the same result. A POST offers no such guarantee and is thus more flexible in its implementation.
Why do web browsers issue a a warning when POST request is resubmitted
A client should be able to resubmit a GET, DELETE or PUT request and expect the same result. A POST offers no such guarantee.
HTML forms only support (which methods?)
GET and POST
PUT requests are most similar to
POST
Use PUT requests when
When implementing update operations such as the U in CRUD
When implementing create operations such as the C in CRUD
Why is it important to follow the conventions with HTTP?
HTTP services are often consumed by clients that are not considered at the time of development.
Also Think of the case where developers implemented delete with simple hyperlinks.
A HEAD Request should be treated the same as a ______ request except that it should not return a ___________________________
GET
Body
If HTTP methods declare the type of action to be performed, then route matchers declare …
the resources on which the action is to be performed.
Name the three types of route matchers that are supported out of the box
Path expressions (string)
Regular expressions
Boolean expressions
The most common type of route matcher is
path expression
A path expression always starts with a
/
get {“/artists”} which is the route matcher
/artists
class RecordStore extends ScalatraServlet { get(“/artists”) {
Artist.getAll.map { artist =>
${artist.name} } } } which is the Action code?
Artist.getAll.map { artist =>
${artist.name} }
How would you parameterise a path expression
use a path parameter (declared by preceding it with a colon character
A path parameter is never …
At least ________ must be matched
an empty string
at least one character must be matched
A path parameter matches everything up to …
the next special character /,?, or #
The “/artists/?” expression would match a request to both of these
■ http://localhost:8080/artists
■ http://localhost:8080/artists/
In the literal URI, ? marks the beginning of
the query string
How is the use of ? different in the literal URI and in a path expression
in a path expression ? means that the preceding character is optional, in a literal URI the ? marks the beginning of a query string
get(“/artists/:name/info.?:format?)
what to the two ?s apply to?
the first ? makes the period optional the second applies to the format param
In this route, you use a single /downloads/* route to serve files under a virtual filesystem. You could have used /downloads/:name, but
ecall that named parameters match up until the next / char- acter in a URL. A splat parameter frees you from this restriction, so that you can match a path in an arbitrary directory structure.
A route matcher returns an
Option[Map[String, Seq[String]]
What does SCALA do to prevent null pointer expressions (NPE)
Uses an Option Type
How would you prevent an object of type String from returning an NPE.
declare it to have a type of Option[String]
what is “implicit”
a compile time decorator for doing type conversions
how does implicit work?
at compile time if there is a cast that the compiler would not know how to do it looks for an implicit function
If you want to use your custom converters across servlets, or if you have more than one of them, it might be a good idea to place them in a
trait
It is possible to run (or not run) filters based on fairly complex conditional code. For example, if you wanted to run a before filter for a specific route, but only on POST requests, you could do this:
before(“/hackers”, request.requestMethod == Post) {
DataBase.connect;
}
It’s possible to use ______________________ to conditionally run filters on your routes.
any boolean expression you can think of
Sometimes you’ll need to read headers off an incoming request. You can do this using :
request.getHeader()
HTTP-based applications can accept input in a variety of ways, including form parameters, path parameters, and query parameters. Scalatra reads all of these types of parameters from incoming requests using the ________ function, which is part of the Scalatra DSL.
params
The params function returns a _______ _____ containing all incoming parameters
Scala map