Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: set middleware for json type #706

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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