Do You Need PUT and PATCH?

Conventional wisdom says that REST APIs should be implemented as follows:

  • GET – read
  • POST – add
  • PUT/PATCH – modify
  • DELETE – remove

This mostly works well. Query parameters can be used to supply arguments for GET and DELETE operations. POSTs can use either URL-encoded or multipart form data, standard encodings supported by nearly all HTTP clients.

However, it doesn’t work quite as well for PUT or PATCH. PUT has no standard encoding, and requires the entire resource to be sent in the payload. PATCH was introduced as a workaround to this limitation, but it also lacks a standard encoding, and is not supported by all clients (notably Java).

However, the POST method can also be used to modify resources. The semantics of POST are less strict than PUT, so it can support partial updates, like PATCH. Further, the same encoding used for creating resources (URL-encoded or multipart) can also be used for updates.

For example:

  • POST /products – add a new resource to the “products” collection using the data specified in the request body
  • POST /products/101 – update the existing resource with ID 101 in the products collection using the (possibly partial) data specified in the request body

This approach works particularly well when resources are backed by relational database tables. An “add” POST maps directly to a SQL INSERT operation, and a “modify” POST translates to a SQL UPDATE. The key/value pairs in the body (whether URL-encoded or multipart) can be mapped directly to the table columns.

The approach also supports bulk inserts and updates. POSTing a URL-encoded payload works well for individual records, but JSON, CSV, or XML could easily be used to add or update multiple records at a time.

So, do you really need PUT and PATCH? Given that POST is more flexible, better supported, and can handle both create and update operations, I’d say no. Please share your thoughts in the comments!

Installing MongoDB on 32-bit Ubuntu 15.10

I recently decided to install MongoDB on an old Mac Mini I use for testing. This particular device is too old to run the latest version of OS X, so I’m currently running the 32-bit version of Ubuntu 15.10 on it. Unfortunately, MongoDB no longer provides installation packages for 32-bit Linux distributions, so I had to set it up manually. Since this was a fairly cumbersome process, I thought I would share the steps I took in case they are of use to anyone:

  1. Download 32-bit MongoDB binaries:
    curl -O
    tar -zxvf mongodb-linux-i686-3.2.4.tgz
  2. Copy MongoDB binaries to /usr/bin:
    sudo cp mongodb-linux-i686-3.2.4/bin/* /usr/bin/
  3. Download startup script:
    curl > init.d
  4. Move startup script to /etc/init.d directory:
    sudo mv init.d /etc/init.d/mongod
    sudo chmod 755 /etc/init.d/mongod        
  5. Create configuration script /etc/mongod.conf:
      dbPath: /var/lib/mongo
        enabled: true
      engine: mmapv1
      destination: file
      logAppend: true
      path: /var/log/mongodb/mongod.log
      fork: true
      port: 27017
  6. Add “mongodb” user:
    sudo useradd --home-dir /var/lib/mongo --shell /bin/false mongodb
    sudo passwd mongodb
  7. Create /var/lib/mongo directory:
    sudo mkdir /var/lib/mongo
    sudo chown -R mongodb /var/lib/mongo
    sudo chgrp -R mongodb /var/lib/mongo
  8. Create /var/log/mongodb directory:
    sudo mkdir /var/log/mongodb
    sudo chown -R mongodb /var/log/mongodb
    sudo chgrp -R mongodb /var/log/mongodb
  9. Create /var/run/ file:
    sudo touch /var/run/
    sudo chown mongodb /var/run/
    sudo chgrp mongodb /var/run/
  10. Initialize service:
    sudo update-rc.d mongod defaults
  11. Restart server:
    sudo shutdown -r now

Installing MySQL on Ubuntu Server

I recently set up a basic MySQL server instance for testing JDBC support in JTemplate. It took me a little while to figure out how to do it, so I wanted to capture the steps in case it is useful to anyone (including my future self):

  • Download and install Ubuntu Server 15.04 (Vivid Vervet)
  • Install Avahi (Bonjour/Zeroconf for Linux) so clients can resolve the server name:
    > sudo apt-get install avahi-daemon
  • Install MySQL:
    > sudo apt-get install mysql-server
  • Edit /etc/mysql/mysql.conf.d/mysqld.cnf to allow remote connections:
  • Allow the MySQL root user to log in remotely:
    > mysql --user=root --password=password
  • Download and install MySQL Workbench

You should now be able to start MySQL Workbench and create a connection to the database server. The host name should be the name you gave to the server when you installed the operating system plus “.local”; e.g. “db.local”.