

You will need to account for every possible client which needs to be supported today. There are two big problems with trying to use user agent for simply determining the response format: All mobile app frameworks and HTTP client libraries that I am aware of have the ability to set this header if needed.

Most Javascript libraries and frontend frameworks will send application/json, but this can usually be explicitly set to something else (e.g.

Web browsers, by default, will send text/html as the value of the Accept header. Accept is explicitly designed to allow the client to state what response format they would like returned. The standard and most future-proof way to set the desired return format for a specific endpoint is to use the Accept header. Use the Accept header or (less ideal) use a separate endpoint/URL. Tldr Do not use the user agent to determine the return format unless absolutely necessary. I know this post is a few years old, but since I stumbled upon it.
