Is it necessary to set a Content-Type in Node.js?
The Content-Type
header is technically optional, but then you are leaving it up to the browser to essentially guess what type of content you are returning. Generally you should always specify a Content-Type
if you know the type (which you probably do).
Content-Type:text/plain forces to download the file
I cannot reproduce this with this script:
<?php
header('Content-Type:text/plain; charset=ISO-8859-15');
echo "This is some text";
However, I can reproduce it with this:
<?php
header('Content-Type:text/plain; charset=ISO-8859-15');
echo "\x00This is some text";
Make sure that your content actually is plain ASCII text...
What are all the possible values for HTTP Content-Type header?
You can find every content types here:
http://www.iana.org/assignments/media-types/media-types.xhtml
The most common types are:
Type application:
application/java-archive
application/EDI-X12
application/EDIFACT
application/javascript
application/octet-stream
application/ogg
application/pdf
application/xhtml+xml
application/x-shockwave-flash
application/json
application/ld+json
application/xml
application/zip
application/x-www-form-urlencodedType audio:
audio/mpeg
audio/x-ms-wma
audio/vnd.rn-realaudio
audio/x-wavType image:
image/gif
image/jpeg
image/png
image/tiff
image/vnd.microsoft.icon
image/x-icon
image/vnd.djvu
image/svg+xmlType multipart:
multipart/mixed
multipart/alternative
multipart/related (using by MHTML (HTML mail).)
multipart/form-dataType text:
text/css
text/csv
text/html
text/javascript (obsolete)
text/plain
text/xmlType video:
video/mpeg
video/mp4
video/quicktime
video/x-ms-wmv
video/x-msvideo
video/x-flv
video/webmType vnd:
application/vnd.android.package-archive
application/vnd.oasis.opendocument.text
application/vnd.oasis.opendocument.spreadsheet
application/vnd.oasis.opendocument.presentation
application/vnd.oasis.opendocument.graphics
application/vnd.ms-excel
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
application/vnd.ms-powerpoint
application/vnd.openxmlformats-officedocument.presentationml.presentation
application/msword
application/vnd.openxmlformats-officedocument.wordprocessingml.document
application/vnd.mozilla.xul+xml
How to set content-type when doing res.send()?
setHeader before sending: https://nodejs.org/api/http.html#http_response_setheader_name_value
res.setHeader('content-type', 'text/plain');
res.send(JSON.stringify({...}));
Do I need a content-type header for HTTP GET requests?
According to the RFC 7231 section 3.1.5.5:
A sender that generates a message containing a payload body SHOULD generate a Content-Type header field in that message unless the intended media type of the enclosed representation is unknown to the sender. If a Content-Type header field is not present, the recipient MAY either assume a media type of "application/octet-stream" ([RFC2046], Section 4.5.1) or examine the data to determine its type.
It means that the Content-Type
HTTP header should be set only for PUT
and POST
requests.
Force Content-Type: text/plain to display in browser page
This sounds like an odd experiment, and you should probably send text/html instead, possibly with pre
markup.
Anyway, octets 88 and 91 (hex.) denote control characters in ISO-8859-1, with no generally accepted assignment. If they are supposed to be printable characters, you should probably declare windows-1252 instead.
What content type to force download of text response?
To be on the safe side and ensure consistent behavior in all browsers, it's usually better to use both:
Content-Type: application/octet-stream
Content-Disposition: attachment;filename=\"My Text File.txt\"
Related Topics
Populate Select Drop Down from a Database Table
PHP Include File in Webroot from File Outside Webroot
Modify an Existing PHP Function to Return a String
PHP Prepared Statement Problems
Disable Template Caching for Development in Opencart 3
Split a Time Range into Pieces by Other Time Ranges
How to Get Value from Form Without Submitting It
PHP Using Declare? What Is a Tick
How to Add Two Strings Together
Fatal Error: Call to Undefined Function Openssl_Random_Pseudo_Bytes()
Illegal Command Error Code 127 in PHP Exec Function
Calling Perl Script from PHP and Passing in Variables, While Also Using Variablized Perl Script Name
Laravel SQLstate[Hy000] [2002] Connection Refused
How to Pass an Array of Pdo Parameters Yet Still Specify Their Types