It is all about address resolution.
Usually, http://localhost:8161 will access http://127.0.0.1:8161. When you make the request to localhost, before making the DNS request, your OS will look at a HOSTS file (/etc/hosts in Linux) and will see there a rule that says that localhost maps to 127.0.0.1.
Sometimes, localhost is mapped to a different IP, such as 127.0.1.1. This may cause the malfunction between localhost and 127.0.0.1. (eg. your service is configured to listen to localhost which is mapped to 127.0.1.1 and you try to access 127.0.0.1).
If you use a hostname, your OS may go further on the network stack, and may even make a DNS request to discover the host IP and then try to access that URL.
When you use the IP address your SO will try to access that URL using the given IP address.