REST API Development with Haskell: Scotty and Servant Quiz Quiz

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.

  1. Haskell Web Frameworks

    Which of the following Haskell frameworks is designed specifically for building REST APIs using a type-driven approach?

    1. Snap
    2. Node
    3. Scotty
    4. Servant

    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.

  2. Routing Syntax

    In Scotty, which keyword is typically used to define routes for handling HTTP GET requests?

    1. getRequest
    2. obtain
    3. fetch
    4. get

    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'.

  3. Route Parameters

    To capture a variable part of a URL in a Scotty route, which syntax is used in the route pattern?

    1. $variable
    2. #variable
    3. :variable
    4. {variable}

    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.

  4. Type Safety in APIs

    Which benefit does Servant offer by encoding the API specification in Haskell's type system?

    1. Static HTML generation
    2. Automatic database creation
    3. Client-side routing only
    4. Compile-time safety

    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.

  5. Returning JSON Data

    When sending JSON responses from a Scotty app, which function should be used to serialize data to JSON?

    1. serializeJSON
    2. jsonify
    3. json
    4. toJson

    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.

  6. Servant Handlers

    In Servant, what is the standard type used to represent handler functions for API endpoints?

    1. Handler
    2. IO
    3. EndPoint
    4. Process

    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.

  7. Defining HTTP Methods

    When defining an API in Servant, which type combinator is used to specify an endpoint that should respond to HTTP POST requests?

    1. Send
    2. Post
    3. Put
    4. Read

    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.

  8. Running the Server

    Which function is commonly used to start a Scotty web application on a specified port?

    1. scotty
    2. startServer
    3. beginScotty
    4. runApp

    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.

  9. Route Matching in Servant

    What happens if a request is made to a Servant API endpoint with the wrong HTTP method for a valid path?

    1. A 405 Method Not Allowed error is returned
    2. The request is silently ignored
    3. A 200 OK response with empty data is sent
    4. A 404 Not Found error is returned

    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.

  10. Describing API Endpoints

    In Servant, which combinator allows you to document an endpoint that returns data in JSON format?

    1. Raw
    2. JSON
    3. Text
    4. Data

    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.