heartcombo/responders

Rails 5 api only returning back empty body

v1nayv opened this issue · 11 comments

This has been raised before
#154

I am using responders version 2.4 and i am getting a empty body when i try to access the api.

Here is the code

class ApplicationController < ActionController::API
  respond_to :json
end
class IndexController < ApplicationController
  def index
    respond_with({test: '123'}.to_json)
    # render json: {test: '123'}.to_json
  end
end

I am responding with a static json which is resulting in a empty body. I have a repo setup to replicate this issue https://github.com/v1nayv/my_api . Access http://localhost:3000/index using POSTMAN or any restful client.

This used to work on prior version of rails.

@v1nayv Hi. I'm getting the same issue. Did you manage to work around the problem? Thanks!

Hi @simonc i have not found a workaround using responders. If your use case allows you to do so i would try removing the responders gem and just use rails render json option.

@v1nayv it worked.

I had to change several things since we have generic endpoints so create must respond with a 201 and destroy with a 204. Thanks

For the create endpoints I used:

render json: resource, serializer: resource_serializer, status: :created

and for the destroy endpoints:

head(:no_content)

I'm having the same problem. I'm using rails 5.2.1 and responders 2.4.0

@brenoperucchi Can you show your routes.rb file?
You should have in you routes.rb file something like:
resources :index, only: :index, defaults: { format: :json }

elDub commented

I have the same results of receiving an empty body in return and found that it was related to a hack done in the Draper gem intercepting render_to_body. Perhaps you are using Draper or another gem or similar hack?

https://github.com/drapergem/draper/blob/master/lib/draper/compatibility/api_only.rb

We were using Draper so it's indeed possible that it was related to it 🤔

Can you provide an example application that reproduce the issue?

Closing as stale. Happy to help if more info is provided, thanks.

I am having the same issue and was wondering if HttpCacheResponder defaults to 204 response. I found this older related issue with a workaround. js-data/js-data-http#16 (comment)

What is the expected behavior? Is this an issue or something that needs custom handling? It seems odd to me that it would be default. For context, a simple health check get request now responds with 204 no_content instead of 200. Our deployment process checks for 200 specifically. Though we could modify or implement a workaround, but it is not ideal and I'd like to be familiar with how the responder is intended to behave.

@carlosantoniodasilva it appears that this happens when the Accept header does not explicitly match a format listed in respond_to. It actually appears to return a 200 instead of a 201 and an empty body. https://github.com/nickhudkins/responders/blob/mime-type-all/test/action_controller/respond_with_api_test.rb#L46-L53 is a failing test to show the behavior.

Providing a default format at the route level requires all controller methods to permit the format explicitly.

As */* as an Accept would imply "I'll take whatever you give me" it does seem odd to return a single empty space. It would seem to me that perhaps we could treat */* to give back whatever the first responds_to type is?