Skip to content

Support aborting requests via a Rack::Response#finish #1192

@elliotlarson

Description

@elliotlarson

I have a post endpoint where I'd like to conditionally return a custom Rack::Response, like so:

post do
  if some_condition?
    hash_of_contextually_relevant_goodness = { foo: 'bar' }
    Rack::Response.new(
      [hash_of_contextually_relevant_goodness], 
      418, 
      { 'Content-Type' => 'application/json' }
    ).finish
  else
    # do some object creation stuff
  end
end

However, this returns the following response body:

[
    418,
    {
        "Content-Length": "13",
        "Content-Type": "application/json"
    },
    {
        "block": {},
        "body": {
            "block": null,
            "body": [
                "{:foo=>\"bar\"}"
            ],
            "chunked": false,
            "header": {
                "Content-Length": "13",
                "Content-Type": "application/json"
            },
            "length": 13,
            "status": 418,
            "writer": {}
        },
        "closed": false
    }
]

It looks like the response is doubling up. What is the correct way to return a custom Rack::Response from my endpoint?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions