From 5ae25f85d95c93e14114b025c1a01b85eb2c1645 Mon Sep 17 00:00:00 2001 From: Romain d'Alverny Date: Fri, 4 Aug 2017 14:08:50 +0200 Subject: [PATCH] Serialize to JSON when content type is set to JSON Until now, the request body is only serialized if it's a Hash. But it could be an Array, or any other serializable type, and those bodies are serialized. See #129, probably #141 as well. I am not sure if this wouldn't break use cases where the body is already expected to be serialized. --- lib/airborne/rest_client_requester.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/airborne/rest_client_requester.rb b/lib/airborne/rest_client_requester.rb index 3ebe68f..220d461 100644 --- a/lib/airborne/rest_client_requester.rb +++ b/lib/airborne/rest_client_requester.rb @@ -7,7 +7,7 @@ def make_request(method, url, options = {}) res = if method == :post || method == :patch || method == :put begin request_body = options[:body].nil? ? '' : options[:body] - request_body = request_body.to_json if options[:body].is_a?(Hash) + request_body = request_body.to_json if is_json_request(headers) RestClient.send(method, get_url(url), request_body, headers) rescue RestClient::Exception => e e.response @@ -24,6 +24,10 @@ def make_request(method, url, options = {}) private + def is_json_request(headers) + headers.fetch(:content_type) == :json + end + def base_headers { content_type: :json }.merge(Airborne.configuration.headers || {}) end