seancribbs/webmachine-ruby — Issue #16: Ability to set the body in a correct way for a POST request? 
Author: jeroenvandijk
Created: 2011-12-01 13:41:40+00:00
State: open

I'm trying to wrap my head around webmachine and I have issues to understand how I can set a body for a POST request in a correct way.

I have the following scenario: I have a POST request that needs to return a body of json. I started with the following two callbacks:

def content_types_provided
[[Webmachine::MediaType.new("application/json", "charset" => "UTF-8", "version" => "1.0"), :to_json]]
end

def post_is_create?
true
end

I would expect that `to_json` is being called on a POST request (as with GET etc). However this is not the case. To make it work I also had to do the following:

def create_path
response.body = to_json
'/path-to-resource'
end

This works when everything is ok, but it is also failing when the client is providing wrong Accept or Content-Type headers. What happens is that a 415 response is returned, but the method `create_path` is still being called so the body is set to the result of `to_json`.

I'm assuming it is my conceptually misunderstanding of the flow of webmachine, but maybe it is something else. Any suggestions?

Thank you,
Jeroen