Return correct API response codes (fixes many TODO comments)

Also, when raising 501 not implemented, return a JSON body just as the other error responses.