<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -843,8 +843,13 @@ module ActiveResource
     #
     #   my_group.to_xml(:skip_instruct =&gt; true)
     #   # =&gt; &lt;subsidiary_group&gt; [...] &lt;/subsidiary_group&gt;
-    def to_xml(options={})
-      attributes.to_xml({:root =&gt; self.class.element_name}.merge(options))
+    def encode(options={})
+      case self.class.format
+        when ActiveResource::Formats[:xml]
+          self.class.format.encode(attributes, {:root =&gt; self.class.element_name}.merge(options))
+        else
+          self.class.format.encode(attributes, options)
+      end
     end
 
     # A method to reload the attributes of this object from the remote web service.
@@ -929,14 +934,14 @@ module ActiveResource
 
       # Update the resource on the remote service.
       def update
-        returning connection.put(element_path(prefix_options), to_xml, self.class.headers) do |response|
+        returning connection.put(element_path(prefix_options), encode, self.class.headers) do |response|
           load_attributes_from_response(response)
         end
       end
 
       # Create (i.e., save to the remote service) the new resource.
       def create
-        returning connection.post(collection_path, to_xml, self.class.headers) do |response|
+        returning connection.post(collection_path, encode, self.class.headers) do |response|
           self.id = id_from_response(response)
           load_attributes_from_response(response)
         end</diff>
      <filename>activeresource/lib/active_resource/base.rb</filename>
    </modified>
    <modified>
      <diff>@@ -63,6 +63,13 @@ module ActiveResource
   # This class is used by ActiveResource::Base to interface with REST
   # services.
   class Connection
+
+    HTTP_FORMAT_HEADER_NAMES = {  :get =&gt; 'Accept',
+      :put =&gt; 'Content-Type',
+      :post =&gt; 'Content-Type',
+      :delete =&gt; 'Accept'
+    }
+
     attr_reader :site, :user, :password, :timeout
     attr_accessor :format
 
@@ -106,25 +113,25 @@ module ActiveResource
     # Execute a GET request.
     # Used to get (find) resources.
     def get(path, headers = {})
-      format.decode(request(:get, path, build_request_headers(headers)).body)
+      format.decode(request(:get, path, build_request_headers(headers, :get)).body)
     end
 
     # Execute a DELETE request (see HTTP protocol documentation if unfamiliar).
     # Used to delete resources.
     def delete(path, headers = {})
-      request(:delete, path, build_request_headers(headers))
+      request(:delete, path, build_request_headers(headers, :delete))
     end
 
     # Execute a PUT request (see HTTP protocol documentation if unfamiliar).
     # Used to update resources.
     def put(path, body = '', headers = {})
-      request(:put, path, body.to_s, build_request_headers(headers))
+      request(:put, path, body.to_s, build_request_headers(headers, :put))
     end
 
     # Execute a POST request.
     # Used to create new resources.
     def post(path, body = '', headers = {})
-      request(:post, path, body.to_s, build_request_headers(headers))
+      request(:post, path, body.to_s, build_request_headers(headers, :post))
     end
 
     # Execute a HEAD request.
@@ -187,12 +194,12 @@ module ActiveResource
       end
 
       def default_header
-        @default_header ||= { 'Content-Type' =&gt; format.mime_type }
+        @default_header ||= {}
       end
 
       # Builds headers for request to remote service.
-      def build_request_headers(headers)
-        authorization_header.update(default_header).update(headers)
+      def build_request_headers(headers, http_method=nil)
+        authorization_header.update(default_header).update(headers).update(http_format_header(http_method))
       end
 
       # Sets authorization header
@@ -200,6 +207,10 @@ module ActiveResource
         (@user || @password ? { 'Authorization' =&gt; 'Basic ' + [&quot;#{@user}:#{ @password}&quot;].pack('m').delete(&quot;\r\n&quot;) } : {})
       end
 
+      def http_format_header(http_method)
+        {HTTP_FORMAT_HEADER_NAMES[http_method] =&gt; format.mime_type}
+      end
+
       def logger #:nodoc:
         ActiveResource::Base.logger
       end</diff>
      <filename>activeresource/lib/active_resource/connection.rb</filename>
    </modified>
    <modified>
      <diff>@@ -30,7 +30,7 @@ module ActiveResource
   #   Person.get(:active)  # GET /people/active.xml
   #   # =&gt; [{:id =&gt; 1, :name =&gt; 'Ryan'}, {:id =&gt; 2, :name =&gt; 'Joe'}]
   #
-  module CustomMethods 
+  module CustomMethods
     def self.included(base)
       base.class_eval do
         extend ActiveResource::CustomMethods::ClassMethods
@@ -83,24 +83,25 @@ module ActiveResource
         &quot;#{prefix(prefix_options)}#{collection_name}/#{method_name}.#{format.extension}#{query_string(query_options)}&quot;
       end
     end
-    
+
     module InstanceMethods
       def get(method_name, options = {})
         connection.get(custom_method_element_url(method_name, options), self.class.headers)
       end
-      
-      def post(method_name, options = {}, body = '')
+
+      def post(method_name, options = {}, body = nil)
+        request_body = body.nil? ? encode : body
         if new?
-          connection.post(custom_method_new_element_url(method_name, options), (body.nil? ? to_xml : body), self.class.headers)
+          connection.post(custom_method_new_element_url(method_name, options), request_body, self.class.headers)
         else
-          connection.post(custom_method_element_url(method_name, options), body, self.class.headers)
+          connection.post(custom_method_element_url(method_name, options), request_body, self.class.headers)
         end
       end
-      
+
       def put(method_name, options = {}, body = '')
         connection.put(custom_method_element_url(method_name, options), body, self.class.headers)
       end
-      
+
       def delete(method_name, options = {})
         connection.delete(custom_method_element_url(method_name, options), self.class.headers)
       end
@@ -110,7 +111,7 @@ module ActiveResource
         def custom_method_element_url(method_name, options = {})
           &quot;#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{id}/#{method_name}.#{self.class.format.extension}#{self.class.send!(:query_string, options)}&quot;
         end
-      
+
         def custom_method_new_element_url(method_name, options = {})
           &quot;#{self.class.prefix(prefix_options)}#{self.class.collection_name}/new/#{method_name}.#{self.class.format.extension}#{self.class.send!(:query_string, options)}&quot;
         end</diff>
      <filename>activeresource/lib/active_resource/custom_methods.rb</filename>
    </modified>
    <modified>
      <diff>@@ -2,22 +2,22 @@ module ActiveResource
   module Formats
     module JsonFormat
       extend self
-      
+
       def extension
         &quot;json&quot;
       end
-      
+
       def mime_type
         &quot;application/json&quot;
       end
-      
-      def encode(hash)
+
+      def encode(hash, options={})
         hash.to_json
       end
-      
+
       def decode(json)
         ActiveSupport::JSON.decode(json)
       end
     end
   end
-end
\ No newline at end of file
+end</diff>
      <filename>activeresource/lib/active_resource/formats/json_format.rb</filename>
    </modified>
    <modified>
      <diff>@@ -2,23 +2,23 @@ module ActiveResource
   module Formats
     module XmlFormat
       extend self
-      
+
       def extension
         &quot;xml&quot;
       end
-      
+
       def mime_type
         &quot;application/xml&quot;
       end
-      
-      def encode(hash)
-        hash.to_xml
+
+      def encode(hash, options={})
+        hash.to_xml(options)
       end
-      
+
       def decode(xml)
         from_xml_data(Hash.from_xml(xml))
       end
-      
+
       private
         # Manipulate from_xml Hash, because xml_simple is not exactly what we
         # want for Active Resource.
@@ -28,7 +28,7 @@ module ActiveResource
           else
             data
           end
-        end      
+        end
     end
   end
-end
\ No newline at end of file
+end</diff>
      <filename>activeresource/lib/active_resource/formats/xml_format.rb</filename>
    </modified>
    <modified>
      <diff>@@ -146,7 +146,7 @@ module ActiveResource
     attr_accessor :path, :method, :body, :headers
 
     def initialize(method, path, body = nil, headers = {})
-      @method, @path, @body, @headers = method, path, body, headers.reverse_merge('Content-Type' =&gt; 'application/xml')
+      @method, @path, @body, @headers = method, path, body, headers.merge(ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[method] =&gt; 'application/xml')
     end
 
     def ==(other_request)</diff>
      <filename>activeresource/lib/active_resource/http_mock.rb</filename>
    </modified>
    <modified>
      <diff>@@ -10,8 +10,7 @@ class CustomMethodsTest &lt; Test::Unit::TestCase
     @ryan  = { :name =&gt; 'Ryan' }.to_xml(:root =&gt; 'person')
     @addy  = { :id =&gt; 1, :street =&gt; '12345 Street' }.to_xml(:root =&gt; 'address')
     @addy_deep  = { :id =&gt; 1, :street =&gt; '12345 Street', :zip =&gt; &quot;27519&quot; }.to_xml(:root =&gt; 'address')
-    @default_request_headers = { 'Content-Type' =&gt; 'application/xml' }
-    
+
     ActiveResource::HttpMock.respond_to do |mock|
       mock.get    &quot;/people/1.xml&quot;,             {}, @matz
       mock.get    &quot;/people/1/shallow.xml&quot;, {}, @matz</diff>
      <filename>activeresource/test/base/custom_methods_test.rb</filename>
    </modified>
    <modified>
      <diff>@@ -819,7 +819,7 @@ class BaseTest &lt; Test::Unit::TestCase
   
   def test_to_xml
     matz = Person.find(1)
-    xml = matz.to_xml
+    xml = matz.encode
     assert xml.starts_with?('&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;')
     assert xml.include?('&lt;name&gt;Matz&lt;/name&gt;')
     assert xml.include?('&lt;id type=&quot;integer&quot;&gt;1&lt;/id&gt;')</diff>
      <filename>activeresource/test/base_test.rb</filename>
    </modified>
    <modified>
      <diff>@@ -5,14 +5,22 @@ class FormatTest &lt; Test::Unit::TestCase
   def setup
     @matz  = { :id =&gt; 1, :name =&gt; 'Matz' }
     @david = { :id =&gt; 2, :name =&gt; 'David' }
-    
+
     @programmers = [ @matz, @david ]
   end
-  
+
+  def test_http_format_header_name
+    header_name = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:get]
+    assert_equal 'Accept', header_name
+
+    headers_names = [ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:put], ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:post]]
+    headers_names.each{|header_name| assert_equal 'Content-Type', header_name}
+  end
+
   def test_formats_on_single_element
     for format in [ :json, :xml ]
       using_format(Person, format) do
-        ActiveResource::HttpMock.respond_to.get &quot;/people/1.#{format}&quot;, {}, ActiveResource::Formats[format].encode(@david)
+        ActiveResource::HttpMock.respond_to.get &quot;/people/1.#{format}&quot;, {'Accept' =&gt; ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@david)
         assert_equal @david[:name], Person.find(1).name
       end
     end
@@ -21,7 +29,7 @@ class FormatTest &lt; Test::Unit::TestCase
   def test_formats_on_collection
     for format in [ :json, :xml ]
       using_format(Person, format) do
-        ActiveResource::HttpMock.respond_to.get &quot;/people.#{format}&quot;, {}, ActiveResource::Formats[format].encode(@programmers)
+        ActiveResource::HttpMock.respond_to.get &quot;/people.#{format}&quot;, {'Accept' =&gt; ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@programmers)
         remote_programmers = Person.find(:all)
         assert_equal 2, remote_programmers.size
         assert remote_programmers.select { |p| p.name == 'David' }
@@ -32,7 +40,7 @@ class FormatTest &lt; Test::Unit::TestCase
   def test_formats_on_custom_collection_method
     for format in [ :json, :xml ]
       using_format(Person, format) do
-        ActiveResource::HttpMock.respond_to.get &quot;/people/retrieve.#{format}?name=David&quot;, {}, ActiveResource::Formats[format].encode([@david])
+        ActiveResource::HttpMock.respond_to.get &quot;/people/retrieve.#{format}?name=David&quot;, {'Accept' =&gt; ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode([@david])
         remote_programmers = Person.get(:retrieve, :name =&gt; 'David')
         assert_equal 1, remote_programmers.size
         assert_equal @david[:id], remote_programmers[0]['id']
@@ -40,13 +48,13 @@ class FormatTest &lt; Test::Unit::TestCase
       end
     end
   end
-  
+
   def test_formats_on_custom_element_method
     for format in [ :json, :xml ]
       using_format(Person, format) do
         ActiveResource::HttpMock.respond_to do |mock|
-          mock.get &quot;/people/2.#{format}&quot;, {}, ActiveResource::Formats[format].encode(@david)
-          mock.get &quot;/people/2/shallow.#{format}&quot;, {}, ActiveResource::Formats[format].encode(@david)
+          mock.get &quot;/people/2.#{format}&quot;, {'Accept' =&gt; ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@david)
+          mock.get &quot;/people/2/shallow.#{format}&quot;, {'Accept' =&gt; ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@david)
         end
         remote_programmer = Person.find(2).get(:shallow)
         assert_equal @david[:id], remote_programmer['id']
@@ -57,20 +65,24 @@ class FormatTest &lt; Test::Unit::TestCase
     for format in [ :json, :xml ]
       ryan = ActiveResource::Formats[format].encode({ :name =&gt; 'Ryan' })
       using_format(Person, format) do
-        ActiveResource::HttpMock.respond_to.post &quot;/people/new/register.#{format}&quot;, {}, ryan, 201, 'Location' =&gt; &quot;/people/5.#{format}&quot;
         remote_ryan = Person.new(:name =&gt; 'Ryan')
+        ActiveResource::HttpMock.respond_to.post &quot;/people.#{format}&quot;, {'Content-Type' =&gt; ActiveResource::Formats[format].mime_type}, ryan, 201, {'Location' =&gt; &quot;/people/5.#{format}&quot;}
+        remote_ryan.save
+
+        remote_ryan = Person.new(:name =&gt; 'Ryan')
+        ActiveResource::HttpMock.respond_to.post &quot;/people/new/register.#{format}&quot;, {'Content-Type' =&gt; ActiveResource::Formats[format].mime_type}, ryan, 201, {'Location' =&gt; &quot;/people/5.#{format}&quot;}
         assert_equal ActiveResource::Response.new(ryan, 201, {'Location' =&gt; &quot;/people/5.#{format}&quot;}), remote_ryan.post(:register)
       end
     end
   end
-  
+
   def test_setting_format_before_site
     resource = Class.new(ActiveResource::Base)
     resource.format = :json
     resource.site   = 'http://37s.sunrise.i:3000'
     assert_equal ActiveResource::Formats[:json], resource.connection.format
   end
-  
+
   private
     def using_format(klass, mime_type_reference)
       previous_format = klass.format</diff>
      <filename>activeresource/test/format_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>e7df4ce001fc5a7612c6b784ba2524f9b31cbc39</id>
    </parent>
  </parents>
  <author>
    <name>Rasik Pandey</name>
    <email>rbpandey@gmail.com</email>
  </author>
  <url>http://github.com/rails/rails/commit/caabe228bc6c6e920043334d717e72093559e118</url>
  <id>caabe228bc6c6e920043334d717e72093559e118</id>
  <committed-date>2008-08-29T18:46:39-07:00</committed-date>
  <authored-date>2008-08-29T18:19:18-07:00</authored-date>
  <message>Format related patches to support serializing data out in the correct format with correct http request headers per http method type [#450 state:resolved]

Signed-off-by: Tarmo T&#228;nav &lt;tarmo@itech.ee&gt;
Signed-off-by: Jeremy Kemper &lt;jeremy@bitsweat.net&gt;</message>
  <tree>86e236390fefbca50a9763e4041d7b9a3b0b85ae</tree>
  <committer>
    <name>Jeremy Kemper</name>
    <email>jeremy@bitsweat.net</email>
  </committer>
</commit>
