how to set array params in get request in rails - ruby-on-rails

I use http get in rails. There are array params in the request. How can I encode the params into the request?
My code is like the next:
uri = URI(url)
uri.query = URI.encode_www_form(params) if params
resp = Net::HTTP.get_response(uri)
data = resp.body
When the params have an array, such as ids, the url will like "http://url?ids=1&ids=2", but I want to the url like "http://url?ids[]=1&ids[]=2".

Related

How can I get the value in a hash of the response of POST request on Ruby?

Now, I am working on a project using post request on Ruby.
I could get the response with the code below.
def send_param(param)
uri = URI.parse('https:~~~~~~~~~~~~')
https = Net::HTTP.new(uri.host, 443)
response = https.post(uri.path, param.to_query)
print response.body
end
And response.body looks like something like this
{"result": {"id":1111, "name": John}}
Now, I need to get the value of John above.
it will be something like this.
response["name"]
but I can't get the value as expected.
So I want you to help me, if you can.
Thank you.
You can parse it and fetch the result after it using the dig method.
def send_param(param)
uri = URI.parse('https:~~~~~~~~~~~~')
https = Net::HTTP.new(uri.host, 443)
response = https.post(uri.path, param.to_query)
JSON.parse(response.body)
end
result = send_param({})
result.dig('result', 'name')

Undefined split method while using request.url

I am trying to get the current url of my page and the parameters that go with it. When I manually put the url in, the undefined split method error does not occur. I have tried the following:
request.url
request.original_url
request.fullpath
adding in
id = request.original_url
and adding this function
def original_url
base_url + original_fullpath
end
What does currently work for me is below, but I cannot have the same url and parameters every time so this method won't work later on.
#parse current url
#what I would like below in comment
#id = request.original_url
url = "http://127.0.0.1:3000/reviews/new?id=2"
uri = URI.parse(url)
params = CGI.parse(uri.query)
id = params['id'].first
Any help or tips on if I'm missing something would be appreciated.
Had the same issue. The absence of query parameters in url was causing it.
url = "http://127.0.0.1:3000/reviews/new" # no query params
uri = URI.parse(url)
query = uri.query # nil
params = CGI.parse(query) # Undefined split Exception
This solves it:
uri = URI.parse(url)
if uri.query
params = CGI.parse(uri.query)
id = params['id'].first
end

How can I parse subpages of another website in Rails?

I'm creating a third-party web application of Last.fm and I'm having an issue with getting info about certain artist from them.
I have a method that parses data about some #{artist} from JSON:
artists_helper.rb
require 'net/http'
require 'json'
module ArtistsHelper
def about(artist)
artist = "?"
url = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=#{artist}&api_key=f5cb791cfb2ade77749afcc97b5590c8&format=json"
uri = URI(url)
response = Net::HTTP.get(uri)
JSON.parse(response)
end
end
If I change '?' to the artist name in that method I can successfully parse info about artist from JSON file of that artist. But for when I go the page e.g. http://localhost:3000/artists/Wild+Nothing I need the method 'about(artist)' to get the value 'Wild+Nothing' and parse the data for Wild Nothing from Last.fm's JSON file.
How can I tell the method 'about' that what stands after http://localhost:3000/artists/ is the required value?
In the routes, have a get route name that accepts a variable
get 'artists/:name', to: 'artists#about'
In the artists controller, have an about function:
def about
artist = params[:name]
url = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=#{artist}&api_key=f5cb791cfb2ade77749afcc97b5590c8&format=json"
uri = URI(url)
response = Net::HTTP.get(uri)
response = JSON.parse(response)
render json: response
end
and we are good to go to display the json on the view.
If you need the param in the helper, just pass params[:name] to the helper as a parameter.
about(param[:name]) #wherever you are calling this method in the controller or view
def about(artist)
url = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=#{artist}&api_key=f5cb791cfb2ade77749afcc97b5590c8&format=json"
uri = URI(url)
response = Net::HTTP.get(uri)
JSON.parse(response)
end

http request and get a json for response in rails

In a rails project, I want send a http request and get a json in response and save it to database. I send http request like below:
def test_json
#url is a address like: http://192.32.10.18:8080/GetJson?serial=4306341
uri = URI.parse(url)
response = Net::HTTP.get_response(uri)
end
Now, I want save this json to database and then show to user, How can I do this?
To get the response body you'd use:
uri = URI.parse(the_url)
response = Net::HTTP.get_response(uri)
At this point response is your response (in your case its a string of JSON) which you can do whatever you want with.

Validating URL by making request

In Rails controller, I'd like to validate a user-inputted URL (say, it's in a variable url) by making a request to it, and checking that the response is not 50X. How can I do that?
You can send request from a controller using Net::Http module as follows
uri = URI('http://festivalsherpa.com')
response = Net::HTTP.get_response(uri)
response.to_hash["status"] will return you proper response code
There is one more way
you can do in more meaningful way like:
uri = URI.parse(your_url)
connection = Net::HTTP.new(uri.host)
request = Net::HTTP::Get.new(uri.request_uri) # For post req, you can replace 'Get' by 'Post'
response = connection.request(request)
You can see what is returned:
p response.body
If the response returned is JSON, then you can do
JSON.parse(response.body) #It will give you hash object
You can check the response code/status
p response.code
If you want to handle status codes, then you can handle it through case statements
case response.code
when "200"
<some code>
when "500"
<some code>
end
And so on..

Resources