diff --git a/lib/twurl/oauth_client.rb b/lib/twurl/oauth_client.rb index 773e333..4fa4408 100644 --- a/lib/twurl/oauth_client.rb +++ b/lib/twurl/oauth_client.rb @@ -109,7 +109,17 @@ def perform_request_from_options(options, &block) request.body = multipart_body.join request.content_type = "multipart/form-data, boundary=\"#{boundary}\"" elsif request.content_type && options.data - request.body = options.data.keys.first + data = '' + + if options.data.length == 1 && options.data.values.first == nil + data = options.data.keys.first + else + options.data.each_with_index do |(key, value), i| + data += (i == 0 ? '' : '&') + key.to_s + (value == nil ? '' : '=' + value.to_s) + end + end + + request.body = data elsif options.data request.set_form_data(options.data) end diff --git a/test/oauth_client_test.rb b/test/oauth_client_test.rb index 94fd85b..e375fd3 100644 --- a/test/oauth_client_test.rb +++ b/test/oauth_client_test.rb @@ -157,6 +157,19 @@ def test_content_type_is_not_overridden_if_set_and_data_in_options client.perform_request_from_options(options) end + def test_post_body_data_is_not_parsed_if_content_type_is_set + + options.request_method = 'post' + options.data = { '{ "data": "do' => nil, 'not' => 'parse', 'me' => '!" }' } + options.headers = { 'Content-Type' => 'application/json' } + + mock(client.consumer.http).request( + satisfy { |req| req.is_a?(Net::HTTP::Post) && req.body == '{ "data": "do¬=parse&me=!" }' } + ) + + client.perform_request_from_options(options) + end + def test_content_type_is_set_to_form_encoded_if_not_set_and_data_in_options client = Twurl::OAuthClient.test_exemplar