We have an API endpoint called by a legacy client with Accept: */* (1), in which case it should return JSON. This API also needs to respond differently to turbo_stream requests. How can these be differentiated?
In the following snippet, the legacy client is sent the turbo_stream response because */* matches every mime type (including turbo_stream), which is not what we want:
respond_to do |format|
format.turbo_stream
format.any { render ... }
end
(1) No header is actually sent, which is interpreted as Accept: */*.
Thanks @kawsay, indeed I can’t set the header as this is to handle calls from a legacy client which is already in the wild. As for your suggestion, what I was looking for was a way to be able to use respond_to. Here is my workaround for now:
if request.format.json? || request.format == "*/*"
render json: ...
else
render turbo_stream: ...
end