I would like to make an API call dynamically. API (Get) result must vary dynamically based on the query string passed.
I have hard coded the API URL without any query string, and supplied the API URL as an instance variable:
@apiRequest = HTTParty.get("http://localhost:1880/api/devices")
@parseApiRequest = JSON.parse(@apiRequest.body)
I want @apiRequest
to change dynamically based on user input from a form (I had already developed a form that gets inputs from user's).
Is it possible to change the value of an instance variable dynamically? User's input value must be passed onto that controller as a query string.
Sample image that gets user input: Getting User Input
The instance variable must be updated dynamically as,
@apiRequest = HTTParty.get("http://localhost:1880/api/devices?device_id=123456")
Kindly suggest.
Simply interpolate the dynamic data into the api request parameter string:
extraParamsOrPath = "assemble this from form inputs"
urlPrefix = "http://localhost:1880/"
@apiRequest = HTTParty.get("#{urlPrefix}/#{extraParamsOrPath}")
And make sure the user input is sanitized ;)