ruby-grape / ruby-grape/grape

Support aborting requests via a Rack::Response#finish

Open
#1,192 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature request
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how the endpoint handles the returned Rack::Response and how Rack::Response#finish is interpreted. Reproduce the post endpoint example, then inspect the request/response handling path for where the response becomes nested. Done means a custom Rack::Response can be returned without its status, headers, and body being serialized again.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.