Skip to content

Conversation

@jodosha
Copy link
Member

@jodosha jodosha commented Nov 27, 2015

According to RFC 2616, when a response MUST have an empty body, it only allows Entity Headers.

For instance, a 204 doesn't allow Content-Type or any other custom header.

This restriction is enforced by Lotus::Action::Head#finish.

However, there are cases that demand to bypass this rule to set meta
informations via headers.

An example is a DELETE request for a JSON API application. It returns a 204 but still wants to specify the rate limit quota via X-Rate-Limit.

require 'lotus/controller'

module Books
  class Destroy
    include Lotus::Action

    def call(params)
      # ...
      self.headers.merge!(
        'Last-Modified' => 'Fri, 27 Nov 2015 13:32:36 GMT',
        'X-Rate-Limit'  => '4000',
        'Content-Type'  => 'application/json',
        'X-No-Pass'     => 'true'
      )

      self.status = 204
    end

    private

    def keep_response_header?(header)
      super || header == 'X-Rate-Limit'
    end
  end
end

Only the following headers will be sent:

  • Last-Modified - because we used `super' in the method that respects the HTTP RFC
  • X-Rate-Limit - because we explicitely allow it

Both Content-Type and X-No-Pass are removed because they're not allowed

If there are no objections, I would release v0.4.6 with only this change, because needed by a project I'm working on.

/cc @lotus/core-team

@jodosha jodosha self-assigned this Nov 27, 2015
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space between { and |

@AlfonsoUceda
Copy link
Contributor

👍

@runlevel5
Copy link

@jodosha 👍

@jodosha jodosha added this to the v0.4.6 milestone Dec 4, 2015
jodosha added a commit that referenced this pull request Dec 4, 2015
…t-body

Allow to force custom headers for responses that shouldn't include them
@jodosha jodosha merged commit 7427712 into master Dec 4, 2015
@jodosha jodosha deleted the custom-headers-for-reponses-without-body branch December 4, 2015 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants