Experiment with an example REST API for a library database.

  1. Download the library database script and REST API
  2. Execute the library.mysql.sql script.

    One way that you can execute the script is using the MySQL command-line client. Open a terminal and enter the following command:

    sudo mysql < library.mysql.sql
    

    This command instructs the mysql client to execute all the SQL statements in the file library.mysql.sql (obviously you must execute this command when in the same directory as this file). A lack of error messages indicates that the SQL statements executed successfully.

    Grant the student user access to the database tables:

    sudo mysql \
        --execute="GRANT ALL PRIVILEGES ON library.* TO 'student'@'localhost';"
    
  3. Create a subdirectory named library in the public_html directory in your home directory on your virtual machine (VM). Place the remaining files in the library directory.
  4. Enable Apache’s mod_rewrite module to manipulate URLs by executing the following commands in the terminal:

    sudo a2enmod rewrite
    sudo systemctl restart apache2
    
  5. Open http://localhost/~student/library/client.html in your web browser. (Remember to replace “student” with your username.)

    If you receive a server error (HTTP status code 500), then Apache’s mod_rewrite is not configured correctly. Contact the instructor for assistance.

  6. Submit a GET request to http://localhost/~student/library/api/books. Examine the response status and content. Do you understand each?

    The status code is 200 (OK) and the response is as follows:

    {
      "9780136006374": "/~student/library/api/books/9780136006374",
      "9780073523323": "/~student/library/api/books/9780073523323",
      "9781305627482": "/~student/library/api/books/9781305627482",
      "9780131873254": "/~student/library/api/books/9780131873254",
      "9780471262978": "/~student/library/api/books/9780471262978",
      "9780470624708": "/~student/library/api/books/9780470624708"
    }
    

    The response indicates each book (identified by its ISBN) and its resource URI, which presumably will return more detailed information about the book.

  7. Submit a GET request for a particular book using resource URI provided in the response to the prior request. How is the book identified? Can you provide the structure of the resource URI?

    The resource URI structure is api/books/:isbn where :isbn indicates the ISBN of a particular book.

    An example request to api/books/9780470624708 returns the following:

    {
      "isbn": "9780470624708",
      "title": "Fundamentals of Database Management Systems",
      "copyright": 2012,
      "publisher": "John Wiley & Sons, Inc."
    }
    
  8. Submit a POST request to http://localhost/~student/library/api/books to create the following entry:

    ISBN
    9781119231509
    Title
    Cybersecurity Law
    Author
    Jeff Kosseff
    Copyright
    2017
    Publisher
    John Wiley & Sons, Inc.

    You must provide this information as JSON as part of of the request body. The format is the same as that returned by a GET request for a specific book. Does the new entry appear when you submit another request that lists all the books?

    The request content should be as follows:

    {
      "isbn": "9781119231509",
      "title": "Cybersecurity Law",
      "copyright": 2017,
      "publisher": "John Wiley & Sons, Inc."
    }
    
  9. Open books.php. Examine the source code to see how the REST API is implemented.
  10. Optional: Add support for the PATCH and PUT methods and test both. A PUT request should replace a resource that currently exists or create the resource if it does not already exist. A PATCH request should update an existing resource.