Making dynamic action

i want make dynamic action , how can i assign method name into a variable?

require ‘http’
endpoint_url=“https://reqres.in/api/users?page=2
endpoint_headers= {“Content-Type” => “application/json”}
#making dynamic action
var = “get”

#without headers
#response = HTTP.get(endpoint_url)

#with headers
response="#{HTTP.headers(endpoint_headers)."#{var}"(endpoint_url)}"

puts response

If what you want is to consume api, I advise you to use better REST Client – simple DSL for accessing HTTP and REST resources

require 'rest-client'
require 'json'

endpoint_url = 'https://reqres.in/api/users?page=2'
method_name = 'get'
endpoint_headers = { 'Content-Type' => 'application/json' }

response = RestClient::Request.execute(method: method_name,
                                       url: endpoint_url,
                                       headers: endpoint_headers)
result = JSON.parse(response)
puts result

Thank you so much ,its helped me lot

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

Sponsor our Newsletter | Privacy Policy | Terms of Service