Explore the fundamentals of building REST APIs using Haskell with Scotty and Servant through this beginner-friendly quiz. Strengthen your understanding of essential concepts, route handling, HTTP methods, and response formatting for modern web services development in Haskell.
Which of the following Haskell frameworks is designed specifically for building REST APIs using a type-driven approach?
Explanation: Servant is known for its type-driven API design, allowing you to specify your API structure and types in Haskell. Scotty is a web microframework focused on simplicity and is not type-driven. Snap is another Haskell web framework but is not designed around type-level APIs. Node is not a Haskell framework.
In Scotty, which keyword is typically used to define routes for handling HTTP GET requests?
Explanation: The 'get' function in Scotty is specifically used to create routes that handle HTTP GET requests. 'fetch' and 'obtain' are not valid functions in Scotty. 'getRequest' does not exist in Scotty's API; the correct, simple form is 'get'.
To capture a variable part of a URL in a Scotty route, which syntax is used in the route pattern?
Explanation: Scotty uses the colon syntax, such as ':variable', to denote route parameters in URL patterns. The options '#variable', '{variable}', and '$variable' are incorrect for Scotty and reflect patterns used in other frameworks or programming languages.
Which benefit does Servant offer by encoding the API specification in Haskell's type system?
Explanation: Servant encodes API specifications in the type system, enabling compile-time safety, so errors in API structure can be caught early. Static HTML generation and automatic database creation are not direct features of Servant. Servant is not limited to client-side routing; it focuses on server-side API handling.
When sending JSON responses from a Scotty app, which function should be used to serialize data to JSON?
Explanation: The 'json' function in Scotty is intended for serializing and sending JSON responses directly. 'toJson' and 'jsonify' are not standard Scotty functions, and 'serializeJSON' is a distractor. Only 'json' is correct in this context.
In Servant, what is the standard type used to represent handler functions for API endpoints?
Explanation: Servant uses the 'Handler' type for endpoint functions, which integrates seamlessly with Servant's infrastructure. 'Process', 'IO', and 'EndPoint' are not the standard types for Servant endpoint handlers. 'IO' is a general Haskell type for side effects but not specific to Servant endpoint handling.
When defining an API in Servant, which type combinator is used to specify an endpoint that should respond to HTTP POST requests?
Explanation: The 'Post' type combinator is explicitly used in Servant to declare a POST endpoint. 'Put' specifies the PUT method, 'Read' and 'Send' are not valid Servant combinators, and they don't relate to HTTP verbs.
Which function is commonly used to start a Scotty web application on a specified port?
Explanation: To run a Scotty app, the 'scotty' function is called with the desired port and the application. 'startServer' and 'runApp' are not part of Scotty's API, while 'beginScotty' is an incorrect function name.
What happens if a request is made to a Servant API endpoint with the wrong HTTP method for a valid path?
Explanation: If a route matches the path but not the HTTP method, Servant returns a 405 Method Not Allowed response. A 404 error indicates the path itself is unknown. Ignoring the request or sending a 200 response with empty data would not be correct or RESTful behavior.
In Servant, which combinator allows you to document an endpoint that returns data in JSON format?
Explanation: The 'JSON' combinator in Servant specifies that an endpoint returns results encoded in JSON. 'Raw' is for serving untyped responses, while 'Data' and 'Text' are not valid Servant combinators for content types. Only 'JSON' properly documents the endpoint’s response format.