Closed
Description
Reproduction:
class API < Grape::API
before do
header "X-Test-A", "Success"
end
class HeaderTest < Grape::Middleware::Base
def before
endpoint = env['api.endpoint']
endpoint.header "X-Test-B", "Success"
end
end
use HeaderTest
get '/' do
present ""
end
end
Expected result:
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 0
Cache-Control: max-age=0, private, must-revalidate
Connection: keep-alive
X-Test-A: Success
X-Test-B: Success
Server: thin
Actual result:
HTTP/1.1 500 Internal Server Error
Content-Type: text/plain
Content-Length: 39
Cache-Control: max-age=0, private, must-revalidate
Connection: keep-alive
Server: thin
undefined method `[]=' for nil:NilClass
Cause: Headers are sent before any Before is called before any response is created.before
calls in middleware
Workaround: Set headers in after
(@app_response[1]
)