# Making dynamic action

**URL:** <https://www.ruby-forum.com/t/making-dynamic-action/253509>\
**Category:** Uncategorized\
**Created:** [December 19, 2019, 9:34am UTC](https://www.ruby-forum.com/t/making-dynamic-action/253509 "2019-12-19T09:34:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunilkumarnagoore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e495f1/32.png) [@sunilkumarnagoore](https://www.ruby-forum.com/u/sunilkumarnagoore)\
**Post date:** [December 19, 2019, 9:34am UTC](https://www.ruby-forum.com/t/making-dynamic-action/253509/1 "2019-12-19T09:34:24Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![arcangel8801](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a87d85/32.png) [@arcangel8801](https://www.ruby-forum.com/u/arcangel8801)\
**Post date:** [December 19, 2019, 2:54pm UTC](https://www.ruby-forum.com/t/making-dynamic-action/253509/2 "2019-12-19T14:54:49Z")

</div>

If what you want is to consume api, I advise you to use better [REST Client – simple DSL for accessing HTTP and REST resources](https://github.com/rest-client/rest-client)

```ruby
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

```

---

<div class="post-metadata">

**Author:** ![sunilkumarnagoore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e495f1/32.png) [@sunilkumarnagoore](https://www.ruby-forum.com/u/sunilkumarnagoore)\
**Post date:** [December 20, 2019, 5:32am UTC](https://www.ruby-forum.com/t/making-dynamic-action/253509/3 "2019-12-20T05:32:29Z")

</div>

Thank you so much ,its helped me lot
