Redirection

By default, rest-man will follow:

  • HTTP 301, 302, 307 redirection requests for GET and HEAD only
  • HTTP 303 redirection requests

RestMan::Response exposes a #history method that returns a list of each response received in a redirection chain.

Get responses in the redirection chain

All calls to RestMan, including Resources, will use the proxy specified by RestMan.proxy.

GET
/test
HTTP/1.1
Accept: */*
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
User-Agent: rest-man/1.1.0 (darwin21 arm64) ruby/3.1.2p20
Host: example.com
HTTP/1.1
301
Moved Permanently
Content-Type: text/plain
Location: http://www.example.com/test
The document has moved to http://www.example.com/test
GET
/test
HTTP/1.1
Accept: */*
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
User-Agent: rest-man/1.1.0 (darwin21 arm64) ruby/3.1.2p20
Host: www.example.com
HTTP/1.1
200
OK
Content-Type: text/plain
Hello world!
simple
advance
resource
cli
RestMan.get "http://example.com/test"

response.code                 #=> 200
response.request.url          #=> http://www.example.com/test

response.history              #=> Arry of RestMan::Response
response.history[0].code      #=> 301
response.history[0].url       #=> http://example.com/test
response = RestMan::Request.execute(
  method: :get,
  url: "http://example.com/test"
)

response.code                 #=> 200
response.request.url          #=> http://www.example.com/test

response.history              #=> Arry of RestMan::Response
response.history[0].code      #=> 301
response.history[0].url       #=> http://example.com/test
resource = RestMan::Resource.new("http://example.com/test")
resposne = resource.get

response.code                 #=> 200
response.request.url          #=> http://www.example.com/test

response.history              #=> Arry of RestMan::Response
response.history[0].code      #=> 301
response.history[0].url       #=> http://example.com/test
$ restman get http://example.com/test

Hello world!

RestMan CLI hasn't support a way to check the history

Disable auto redirection

To disable automatic redirection, set :max_redirects => 0.

GET
/test
HTTP/1.1
Accept: */*
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
User-Agent: rest-man/1.1.0 (darwin21 arm64) ruby/3.1.2p20
Host: example.com
HTTP/1.1
301
Moved Permanently
Content-Type: text/plain
Location: http://www.example.com/test
The document has moved to http://www.example.com/test
simple
advance
resource
cli
RestMan simple mode hasn't support disable auto redirection.
Please use advance mode.
begin
  RestMan::Request.execute(
    method: :get,
    url: "http://example.com/test",
    max_redirects: 0
  )
rescue => error
  error                             #=> 301 Moved Permanently
  error.response.code               #=> 301
  error.response.headers[:location] #=> http://www.example.com/test
  error.response.body               #=> The document has moved to http://www.example.com/test
end 
RestMan Resource hasn't support disable auto redirection.
Please use advance mode.
RestMan CLI hasn't support disable auto redirection.

Manually follow redirection

To manually follow redirection, you can call Response#follow_redirection. Or you could of course inspect the result and choose custom behavior.

GET
/test
HTTP/1.1
Accept: */*
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
User-Agent: rest-man/1.1.0 (darwin21 arm64) ruby/3.1.2p20
Host: example.com
HTTP/1.1
301
Moved Permanently
Content-Type: text/plain
Location: http://www.example.com/test
The document has moved to http://www.example.com/test
GET
/test
HTTP/1.1
Accept: */*
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
User-Agent: rest-man/1.1.0 (darwin21 arm64) ruby/3.1.2p20
Host: www.example.com
HTTP/1.1
200
OK
Content-Type: text/plain
Hello world!
simple
advance
resource
cli
RestMan simple mode hasn't support disable auto redirection and manual follow redirection.
Please use advance mode.
begin
  RestMan::Request.execute(
    method: :get,
    url: "http://example.com/test",
    max_redirects: 0
  )
rescue => error
  error.response.code               #=> 301
  error.response.headers[:location] #=> http://www.example.com/test

  response = error.response.follow_redirection
  response.code                     #=> 200
  response.request.url              #=> http://www.example.com/test
end 
RestMan Resource hasn't support disable auto redirection and manual follow redirection.
Please use advance mode.
RestMan CLI hasn't support manual follow redirectino.

Follow redirection for POST request

Follow redirections for all request types and not only for get and head

RFC : If the 301, 302 or 307 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

POST
/resource
HTTP/1.1
Accept: */*
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Host: example.com
User-Agent: rest-man/1.1.0 (darwin21 arm64) ruby/3.1.2p20
Content-Type: text/plain
Content-Length: 14
Hello, world!
HTTP/1.1
301
Moved Permanently
Content-Type: text/plain
Location: http://www.example.com/resource
The document has moved to http://www.example.com/resource
POST
/resource
HTTP/1.1
Accept: */*
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Host: www.example.com
User-Agent: rest-man/1.1.0 (darwin21 arm64) ruby/3.1.2p20
Content-Type: text/plain
Content-Length: 14
Hello, world!
HTTP/1.1
200
OK
Content-Type: text/plain
Text received.
simple
advance
resource
cli
# block style
RestMan.post("http://example.com/resource", "Hello, world!", { content_type: "text/plain" }) do |response, request, result|
  case response.code
  when 301, 302, 307
    response.follow_redirection
  else
    response.return!
  end
end

# exception style by explicit classes
begin
  RestMan.post("http://example.com/resource", "Hello, world!", { content_type: "text/plain" }) 
rescue RestMan::MovedPermanently, RestMan::Found, RestMan::TemporaryRedirect => error
  error.response.follow_redirection
end

# exception style by response code
begin
  RestMan.post("http://example.com/resource", "Hello, world!", { content_type: "text/plain" }) 
rescue RestMan::ExceptionWithResponse => error
  case error.http_code
  when 301, 302, 307
    error.response.follow_redirection
  else
    raise
  end
end
# block style
RestMan::Request.execute(
  method: :post,
  url: "http://example.com/resource",
  payload: "Hello, world!",
  headers: { content_type: "text/plain" }
) do |response, request, result|
  case response.code
  when 301, 302, 307
    response.follow_redirection
  else
    response.return!
  end
end

# exception style by explicit classes
begin
  RestMan::Request.execute(
    method: :post,
    url: "http://example.com/resource",
    payload: "Hello, world!",
    headers: { content_type: "text/plain" }
  )
rescue RestMan::MovedPermanently, RestMan::Found, RestMan::TemporaryRedirect => error
  error.response.follow_redirection
end

# exception style by response code
begin
  RestMan::Request.execute(
    method: :post,
    url: "http://example.com/resource",
    payload: "Hello, world!",
    headers: { content_type: "text/plain" }
  )
rescue RestMan::ExceptionWithResponse => error
  case error.http_code
  when 301, 302, 307
    error.response.follow_redirection
  else
    raise
  end
end
resource = RestMan::Resource.new("http://example.com/resource")

# block style
resource.post("Hello, world!", {content_type: "text/plain"}) do |response, request, result|
  case response.code
  when 301, 302, 307
    response.follow_redirection
  else
    response.return!
  end
end

# exception style by explicit classes
begin
  resource.post("Hello, world!", {content_type: "text/plain"})
rescue RestMan::MovedPermanently, RestMan::Found, RestMan::TemporaryRedirect => error
  error.response.follow_redirection
end

# exception style by response code
begin
  resource.post("Hello, world!", {content_type: "text/plain"})
rescue RestMan::ExceptionWithResponse => error
  case error.http_code
  when 301, 302, 307
    error.response.follow_redirection
  else
    raise
  end
end
RsetMan CLI doesn't offer a way to follow redirection. You have to doing it manually.