'Being able to specify a tag query parameter when fetching from an existing API using RestClient for Ruby on Rails
I'm using RestClient to fetch from an external API that has a list of blog posts. I need to be able to specify the "tags" attribute so that only posts that contain that tag appear in the JSON response. For example, navigating to localhost:3000/posts?tag=tech will only show posts with tech tag.
Right now, this is what I have in my posts controller, which is static and will only give me posts with a tech tag. How do I make this dynamic so any of the tags will work with my /posts route? I know that it has something to do with specifying params in the url, but I haven't used RestClient before and could use some guidance!
class PostsController < ApplicationController
require 'rest-client'
def index
url = "https:example.com/blog/posts?tag=tech"
response = RestClient.get(url)
render json: response, status: :ok
end
def ping
render json: { success: true }, status: :ok
end
end
Here's also my routes file for reference:
Rails.application.routes.draw do
resources :posts
get '/ping', to: "posts#ping"
end
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
