Skip to content

Instantly share code, notes, and snippets.

@radar
Created January 20, 2011 09:21
Show Gist options
  • Save radar/787626 to your computer and use it in GitHub Desktop.
Save radar/787626 to your computer and use it in GitHub Desktop.

When I make a POST request to /api/v1/projects.json it returns an incorrect Location header. In my opinion, this should point to /api/v1/projects/1, not /projects/1, as APIs can then rely on this header to know how to re-locate this object if need be.

{
"Location"=>"http://example.org/projects/1", # WRONG WRONG WRONG
"Content-Type"=>"application/json; charset=utf-8",
"Cache-Control"=>"no-cache",
"X-UA-Compatible"=>"IE=Edge,chrome=1",
"X-Runtime"=>"0.012087"
}
def create
project = Project.create(params[:project])
respond_with(project, :location => api_v1_project_path(project) )
end
@pixeltrix
Copy link

Can't you override api_location in the controller?

@radar
Copy link
Author

radar commented Jan 20, 2011

apparently the right way to do this is to use sikachu's :location option. Works for me!

@sikachu
Copy link

sikachu commented Jan 20, 2011

@pixeltrix

Nope, as it's in the ActionController::Responder and not in the controller's context. ;)

@pixeltrix
Copy link

@sikachu you're right - what I should've said is that you can subclass ActionController::Responder and override it there. You can then assign your custom responder using self.responder = ApiResponder in your controller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment