header() in Ruby

By: Jeeva Emailed: 1700 times Printed: 2205 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

header(options = "text/html")

Create an HTTP header block as a string.

Includes the empty line that ends the header block.

options can be a string specifying the Content-Type (defaults to text/html), or a hash of header key/value pairs. The following header keys are recognized:

type: the Content-Type header. Defaults to "text/html"
charset: the charset of the body, appended to the Content-Type header.
nph: a boolean value. If true, prepend protocol string and status code, and date; and sets default values for "server" and "connection" if not explicitly set.
status: the HTTP status code, returned as the Status header. See the list of available status codes below.
server: the server software, returned as the Server header.
connection: the connection type, returned as the Connection header (for instance, "close".
length: the length of the content that will be sent, returned as the Content-Length header.
language: the language of the content, returned as the Content-Language header.
expires: the time on which the current content expires, as a Time object, returned as the Expires header.
cookie: a cookie or cookies, returned as one or more Set-Cookie headers. The value can be the literal string of the cookie; a CGI::Cookie object; an Array of literal cookie strings or Cookie objects; or a hash all of whose values are literal cookie strings or Cookie objects. These cookies are in addition to the cookies held in the @output_cookies field.

Other header lines can also be set; they are appended as key: value.

  header
    # Content-Type: text/html

  header("text/plain")
    # Content-Type: text/plain

  header("nph"        => true,
         "status"     => "OK",  # == "200 OK"
           # "status"     => "200 GOOD",
         "server"     => ENV['SERVER_SOFTWARE'],
         "connection" => "close",
         "type"       => "text/html",
         "charset"    => "iso-2022-jp",
           # Content-Type: text/html; charset=iso-2022-jp
         "length"     => 103,
         "language"   => "ja",
         "expires"    => Time.now + 30,
         "cookie"     => [cookie1, cookie2],
         "my_header1" => "my_value"
         "my_header2" => "my_value")

The status codes are:

  "OK"                  --> "200 OK"
  "PARTIAL_CONTENT"     --> "206 Partial Content"
  "MULTIPLE_CHOICES"    --> "300 Multiple Choices"
  "MOVED"               --> "301 Moved Permanently"
  "REDIRECT"            --> "302 Found"
  "NOT_MODIFIED"        --> "304 Not Modified"
  "BAD_REQUEST"         --> "400 Bad Request"
  "AUTH_REQUIRED"       --> "401 Authorization Required"
  "FORBIDDEN"           --> "403 Forbidden"
  "NOT_FOUND"           --> "404 Not Found"
  "METHOD_NOT_ALLOWED"  --> "405 Method Not Allowed"
  "NOT_ACCEPTABLE"      --> "406 Not Acceptable"
  "LENGTH_REQUIRED"     --> "411 Length Required"
  "PRECONDITION_FAILED" --> "412 Precondition Failed"
  "SERVER_ERROR"        --> "500 Internal Server Error"
  "NOT_IMPLEMENTED"     --> "501 Method Not Implemented"
  "BAD_GATEWAY"         --> "502 Bad Gateway"
  "VARIANT_ALSO_VARIES" --> "506 Variant Also Negotiates"

This method does not perform charset conversion.


Ruby Home | All Ruby Tutorials | Latest Ruby Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Jeeva
out() in Ruby
header() in Ruby
unescapeHTML() in Ruby
unescapeElement() in Ruby
URL decode a string in Ruby
Format a Time object as a String in Ruby
pretty() in Ruby
parse(query) in Ruby
new(type = "query") in Ruby
escapeHTML() in Ruby
escapeElement() in Ruby
URL encode a string in Ruby
CGI class in Ruby

More Tutorials in Ruby
Standard Library Packages in Ruby
Module Abbrev in Ruby
encoding and decoding in Ruby using Base64 Module
Benchmark module in Ruby
benchmark(caption = "", label_width = nil, fmtstr = nil, *labels) {|report| ...} in Ruby
bm(label_width = 0, *labels) {|report| ...} in Ruby
bmbm(width = 0) {|job| ...} in Ruby
CGI class in Ruby
URL encode a string in Ruby
escapeElement() in Ruby
escapeHTML() in Ruby
new(type = "query") in Ruby
parse(query) in Ruby
pretty() in Ruby
Format a Time object as a String in Ruby

More Latest News
Most Viewed Articles (in Ruby )
date and time in Ruby
unescapeElement() in Ruby
unescapeHTML() in Ruby
header() in Ruby
Using Forwardable in Ruby
Getting started with Ruby on rails
Prompts, Command Lines, Prompts, and irb in Ruby on rails
Naming conventions in Ruby on rails
Command-line Arguments in Ruby on rails
Methods in Ruby on rails
if . . . elsif . . . else in Ruby on rails
unless in Ruby on rails
The Question Mark Operator in Ruby on rails
Standard Library Packages in Ruby
Module Abbrev in Ruby
Most Emailed Articles (in Ruby)
dRuby client/server mode sample program
Prompts, Command Lines, Prompts, and irb in Ruby on rails
if . . . elsif . . . else in Ruby on rails
encoding and decoding in Ruby using Base64 Module
benchmark(caption = "", label_width = nil, fmtstr = nil, *labels) {|report| ...} in Ruby
SimpleDelegator in Ruby
POST a form in Ruby
Sending emails using SMTP in Ruby
Using Forwardable in Ruby
Getting started with Ruby on rails
Naming conventions in Ruby on rails
Command-line Arguments in Ruby on rails
Methods in Ruby on rails
unless in Ruby on rails
The Question Mark Operator in Ruby on rails