Skip to content

Commit

Permalink
chore: set middleware for json type (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiwarishubham635 authored Jan 17, 2024
1 parent 50b4e19 commit f4d373c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/twilio-ruby/framework/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
10 changes: 9 additions & 1 deletion lib/twilio-ruby/http/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit f4d373c

Please sign in to comment.