Is there an enum for the ContentType property on a HttpWebResponse ("text/plain", "application/octet-stream" etc.)?

Viewed 37340

The closest thing I could find was System.Net.Mime.MediaTypeNames but that doesn't seem to have everything (like json) since it seems to be more focused around email attachments.

3 Answers

IANA's database is most likely to be complete. Currently, they have the list available in CSV format at https://www.iana.org/assignments/media-types/application.csv. I am assuming this is a stable URL whose content changes as updates are made. If you want to stay up to date, you'd need to put together a mechanism that is appropriate for your needs.

There is also the mime.types file that comes with Apache which seems to have been derived from the said list.

If like me you wanted to have no hard-coded string in your code you can use something like below

httpHeaders.add(HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON_VALUE);

which is essentially httpHeaders.add("Content-Type","application/json");

Related