Home > Enterprise >  Ruby's Faraday - Send optional parameters in get method
Ruby's Faraday - Send optional parameters in get method

Time:02-18

I have an endpoint with multiple optional parameters.

def get_customers(params=nil)
   if params.nil?
      customer_url = "#{@url}/customers"
      # call api
      response = connection.get(customer_url)
   else
     # I do not know how to write this part
   end   
end

Could you please help me in order to write a call to and endpoint with optional parameters. The params argument is a hash (key, pair value). The query can have 8 parameters. I do not know how to concatenate the params to the url. I am stack in this section. I am a rookie at ruby and faraday.

Thanks in advance

CodePudding user response:

You don't have to concatenate params with the url on your own. Faraday can accept a hash of params (or nil). You can pass them to the get method like so:

response = connection.get(customer_url, params)

Have into the "GET, HEAD, DELETE, TRACE" section of the documentation for more examples.

Side note: you don't event have to concatenate url and the path. You can pass them as separate arguments.

  • Related