When to use text/plain instead of text/html?

Viewed 6523

When is it recommended to use text/plain content type over text/html.

Imagine I'm developing an endpoint which is consumed in an async manner (ajax) and only returns a string. Am I supposed to use the text/plain content type in this case?

Is it safe to say that text/html should only be used if the response body contains HTML tags?

2 Answers

With text/plain, you can only send text file content

With text/HTML you can send HTML file content, allowing markup formatting

It's that simple...

Don't overthink it, it's quite self explantory

Text:

<b>some bold text</b>

HTML:

some bold text

If you want to render the string returned by API in browser as it is then you can use text/plain. Else If you want browser to render you content as html then use text/html.

For Example, if <b>Hi there</b>is returned from API.

So text/plain will print <b>Hi there</b>, but text/html will print Hi There.

Related