Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow overriding Content-Type in response #97

Closed
bshanks opened this issue Dec 9, 2011 · 10 comments
Closed

allow overriding Content-Type in response #97

bshanks opened this issue Dec 9, 2011 · 10 comments

Comments

@bshanks
Copy link

bshanks commented Dec 9, 2011

How can I override Grape's choice of the Content-Type header in the response? (perhaps this should be added to the README). thanks

@dblock
Copy link
Member

dblock commented Dec 10, 2011

I haven't tried with content-type (it may be overwritten somewhere, in which case you'll need to do some work not to), but we append a last-modified header like this.

header("Last-Modified", ...)

The header method comes from endpoint.rb, hasn't been documented, but I don't see why not. It would be nice if someone (you?) wrote a test for this and content-type and added it to the doc.

# Set an individual header or retrieve
    # all headers that have been set.
    def header(key = nil, val = nil)
      if key
        val ? @header[key.to_s] = val : @header.delete(key.to_s)
      else
        @header
      end
    end

@mbleigh
Copy link
Contributor

mbleigh commented Dec 12, 2011

I actually ran into this myself recently...right now the Grape formatter may well override your Content-Type header without an easy way to get past it. I'll try to take a look at how to expose this functionality soon.

@dblock
Copy link
Member

dblock commented Jan 29, 2012

A related feature was committed in #125 that lets you add content types by extension. @bshanks - what's your scenario?

@dblock
Copy link
Member

dblock commented Feb 14, 2012

We've implemented this by mime-type. This remains a valid feature request to allow to override the content-type in the API implementation itself. For example you fetch /something.image and that can come back as image/jpeg or image/png depending on the mime type of the actual image.

@zeeshanlakhani
Copy link

Have there been any updates on this feature? I'm running into the problem of serving up both json and jsonp responses (if a callback is given). I want to keep the API default format as application/json, but would also like to be able to serve application/javascript for jsonp needs on API requests that contain params[:callback]. Any workarounds for this?

I'm using grape within a Rails 3.2.6 application.

dblock added a commit to dblock/grape that referenced this issue Jul 9, 2012
@dblock
Copy link
Member

dblock commented Jul 9, 2012

I made a pull request, #202.

@dblock
Copy link
Member

dblock commented Jul 9, 2012

I think that maybe a cleaner solution for the JSONP problem, specifically, is to add a proper content-type for application/javascript and make all .js API responses be JSONP instead of relying on a callback parameter.

Otherwise, I believe the rack middleware described in #28 just does it.

@zeeshanlakhani
Copy link

Thank you. I agree with you on the cleaner solution. I'm using the middleware, but am presenting via Rabl templates. Rabl is looking for the format defined in my Grape setup. I'm guessing that using Grape/Rabl may be causing a bit of the problem as well.

dblock added a commit that referenced this issue Jul 11, 2012
Implements #97: allow overriding Content-Type.
@dblock
Copy link
Member

dblock commented Jul 11, 2012

#202 was merged, fyi

@zeeshanlakhani
Copy link

Thank you so much.

@dblock dblock closed this as completed Sep 23, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants