From f4d373c7c97a211eeaf0d5f81a5329c97ba89248 Mon Sep 17 00:00:00 2001 From: Shubham Date: Wed, 17 Jan 2024 12:33:14 +0530 Subject: [PATCH] chore: set middleware for json type (#706) --- lib/twilio-ruby/framework/request.rb | 9 ++++++++- lib/twilio-ruby/http/http_client.rb | 10 +++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/twilio-ruby/framework/request.rb b/lib/twilio-ruby/framework/request.rb index 22294fd20..ad5b6e61a 100644 --- a/lib/twilio-ruby/framework/request.rb +++ b/lib/twilio-ruby/framework/request.rb @@ -32,7 +32,14 @@ def to_s data = '' unless @data.nil? || @data.empty? data = @method.equal?('GET') ? "\n -G" : "\n" - data += @data.each.map { |key, value| "-d \"#{key}\"=\"#{value}\"" }.join("\n") + data += case @headers['Content-Type'] + when 'application/x-www-form-urlencoded' + @data.each.map { |key, value| "-d \"#{key}\"=\"#{value}\"" }.join("\n") + when 'application/json' + "-d '#{JSON.generate(@data)}'" + else + @data.each.map { |key, value| "-d \"#{key}\"=\"#{value}\"" }.join("\n") + end end "#{auth} #{@method} #{@url}#{params}#{data}#{headers}" diff --git a/lib/twilio-ruby/http/http_client.rb b/lib/twilio-ruby/http/http_client.rb index 0a0607012..18a07e8b0 100644 --- a/lib/twilio-ruby/http/http_client.rb +++ b/lib/twilio-ruby/http/http_client.rb @@ -27,9 +27,17 @@ def configure_connection(&block) end def _request(request) # rubocop:disable Metrics/MethodLength + middle_ware = case request.headers['Content-Type'] + when 'application/json' + :json + when 'application/x-www-form-urlencoded' + :url_encoded + else + :url_encoded + end @connection = Faraday.new(url: request.host + ':' + request.port.to_s, ssl: { verify: true }) do |f| f.options.params_encoder = Faraday::FlatParamsEncoder - f.request :url_encoded + f.request(middle_ware) f.headers = request.headers if Faraday::VERSION.start_with?('2.') f.request(:authorization, :basic, request.auth[0], request.auth[1])