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

Override content type #412

Closed
dblessing opened this issue May 26, 2013 · 17 comments
Closed

Override content type #412

dblessing opened this issue May 26, 2013 · 17 comments

Comments

@dblessing
Copy link

In a project I'm working on we are having some issues with a recent change (#347). We have a JSON API that has some calls that return RAW content (raw blobs of code). Prior to #347, this was no problem. But now, it puts the entire content in quotes and all new lines are encoded as \n.

Is there a way to override the content type for certain APIs so that this type of content is delivered in plain text? Thanks.

@dblock
Copy link
Member

dblock commented May 26, 2013

Might want to bring up things like this on the list.

I think all you need to do is set content type with content_type "text/plain" when returning raw text or content_type "application/octet-stream" for random binary content. Make sure any such content type is also declared, eg. content_type :binary, "application/octet-stream".

Reopen with a repro if this is still an issue.

@dblock dblock closed this as completed May 26, 2013
@dzaporozhets
Copy link

+1. Adding content_type "text/plain" does not help me. Looks like it still got to_json before render

@dblock
Copy link
Member

dblock commented May 27, 2013

Confirming it's a bug.

@dzaporozhets
Copy link

@dblock thank you

@dblock dblock closed this as completed in dce96e1 May 27, 2013
@dblock
Copy link
Member

dblock commented May 27, 2013

Fixed in dce96e1, please let me know if that works. There's a workaround for existing versions, do env['api.format'] = :txt inside the API.

@dzaporozhets
Copy link

@dblock thank you a lot. env['api.format'] = :txt works great.
But pointing to dce96e1 doesnt work for me.

@dzaporozhets
Copy link

Anyway thank you because I applied env['api.format'] = :txt gitlabhq/gitlabhq@9351a29#L2R127 and everything is ok now :)

@dblock
Copy link
Member

dblock commented May 27, 2013

I want to make sure this does work when the next version is released. Double-check that you're pointing to Grape HEAD.

I added a demo to my little rack app, dblock/grape-on-rack@39a166e and it worked OK.

You can monkey patch the formatter to see what's going on.

module Grape
  module Middleware
    class Formatter < Base
      def after
        status, headers, bodies = *@app_response
        puts "headers: #{headers}"
        puts "content type: #{headers['Content-Type']}"
        puts "mime type: #{mime_types[headers['Content-Type']]}"
        puts "api format: #{env['api.format']}"
        raise "stop here"      
      end
    end
  end
end

The above should have the correct mime type, :txt for text.

@dzaporozhets
Copy link

I want to make sure this does work when the next version is released. Double-check that you're pointing to Grape HEAD.

done

You can monkey patch the formatter to see what's going on.

Thanks. I'll do

@dzaporozhets
Copy link

HEAD

Started GET "/api/v3/projects/15/repository/commits/72d5a566b46db918b17a1040b6e6d0a3dd986386/blob?filepath=Gemfile.lock"

headers: {"Content-Type"=>"text/plain"}
content type: text/plain
mime type: 
api format: json
RuntimeError (stop here):

HEAD + env['api.format'] = :txt

Started GET "/api/v3/projects/15/repository/commits/72d5a566b46db918b17a1040b6e6d0a3dd986386/blob?filepath=Gemfile.lock"

headers: {"Content-Type"=>"text/plain"}
content type: text/plain
mime type: 
api format: txt

@dblock
Copy link
Member

dblock commented May 27, 2013

Do you have a content_type :txt, 'text/plain' declared for the API itself? When you do format :json that removes all default known content types and formatters.

@dzaporozhets
Copy link

nope. Added. See output:

HEAD + content_type :text, 'text/plain' inside API class

headers: {"Content-Type"=>"text/plain"}
content type: text/plain
mime type: text
api format: json

@dblock
Copy link
Member

dblock commented May 27, 2013

Sorry, change :text to :txt. It doesn't have a formatter for :text.

@dzaporozhets
Copy link

Also you may want to look at API class https://github.com/gitlabhq/gitlabhq/blob/master/lib/api/api.rb

@dzaporozhets
Copy link

HEAD + content_type :txt, 'text/plain' inside API class

headers: {"Content-Type"=>"text/plain"}
content type: text/plain
mime type: txt
api format: json

@dblock
Copy link
Member

dblock commented May 27, 2013

I looked at your code. Here's my diff:

Gemfile

gem "grape", :git => "https://github.com/intridea/grape.git"

api.rb

    format :json
    content_type :txt, 'text/plain'

    get 'foo' do
      content_type 'text/plain'
      "hello world"
    end

spec/requests/api/foo_spec.rb

require 'spec_helper'

describe API::API do
  include ApiHelpers

  it "foo" do
    get api("/foo")
    response.body.should == "hello world"
  end
end

This works. What do you have?

@dzaporozhets
Copy link

Ok I can confirm it works now. Thank you very much! I owe you a 🍺 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants