Make a GET Request and receive parameters - ruby-on-rails

I am working on a Ruby on Rails App.I have to make a GET request to following:
https://[#subdomain].chargify.com/subscriptions/[#subscription.id].json
The response received is going to be json. I am trying to achieve this by making the GET request using the 'net/http' library of ruby and my code is as follows :
res = Net::HTTP.get(URI.parse('https://[#subdomain].chargify.com/subscriptions/[#subscription_id].json'))
But I am getting an error which is :
bad URI(is not URI?):
https://[#subdomain].chargify.com/subscriptions/[#subscription_id].json
I am not sure where exactly am I making mistake with this. Any suggestions or help would be appreciated.
Thanks

Do you mean #{#subdomain} instead of [#subdomain]? That has to be inside of double quotes " in order to get interpolated properly.

By now you know that your error was that you didn't interpolate the string correctly. You should have specified #{#subdomain} rather than [#subdomain] and you need double quotes around the string
The problem that you now have is that you need to tell the http connection to use ssl. I use something like this ..
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)

Two things:
The value of #subdomain needs to be passed using
#{#subdomain}
The connection is ssl
def net_http(uri)
h = Net::HTTP.new(uri.host, uri.port)
h.use_ssl=true
h
end
Then
request=net_http(URI.parse("https://# {#subdomain}.chargify.com/subscriptions/#{#subscription_id}.json"))
To perform get actions
request=net_http(URI.parse("https://# {#subdomain}.chargify.com/subscriptions/#{#subscription_id}.json")).start do |http|
http.get(URI.parse("whatever_url").path,{:params=>123})
end

Related

Ruby on rails HTTP request issue

I am an newbie to Ruby on Rails. I have a url which points to a JSON output. When I ran the URL directly like http://user:pass#myurl.com/json, I am getting the response without any authendication. However http://myurl.com/json requires a username and password through a standard apache pop up authentication box. I have tried to access this URL from my rails controller like the following:
result = JSON.parse(open("http://user:pass#myurl.com/json").read)
When I try to do, I just get an error which says ArgumentError, userinfo not supported. [RFC3986]
Also I have tried the below one. I am getting a 401-Unauthorized error
open("http://...", :http_basic_authentication=>[user, password])
How can I make a request that works in this case. Any help would be appreciated.
You need to use Net::HTTP (or some other HTTP client).
require 'net/http'
require 'uri'
require 'json'
uri = URI('http://myurl.com/json')
req = Net::HTTP::Get.new( uri )
req.basic_auth 'user', 'pass'
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
result = JSON.parse(res.body)
puts result

Using Net::HTTP to POST Json data to remote server

I'm Using ruby on rails and for some reason the remote server doesn't accept the format of my data, I want to make sure nothing else is wrong with with my code:
I have tried your code with following and it's returning data, you can ignore the line request.body or you can use it, in both cases it will return you the response.
uri=URI.parse('https://mkpartners.secure.force.com/services/apexrest/careers?firstName=ss&lastName=dd&email=pp#ss.com')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
request.body =JSON.pretty_generate(data)
response = http.request(request)
The issue with the return value they are returning HTML, when you will try to prase the response using JSON.parse(response.body), it's throwing error.you need to confirm with other team that they should return json.
Problem solved, it was a webservice problem.

Parsing HTTPS instead of HTTP in Rails

I know how to parse HTTP requrests using Net::HTTP. How do you parse HTTPS requests?
I've tried and it says as a response (one of the lines):
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
stox
Edited:
So to show you in a little bit more detail:
For example, here is the HTTP API
https://xxxxx.info/merchant/balance?password=$main_password
How do I use Net:http to run this? Or do I use something else?
Thanks
Stox
would be helpful if you showed us some code?
try setting these attributes
# assuming http is generated like this http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
# might need this as well?
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
You can achieve that by setting use_ssl to true before executing the request.
uri = URI(https://xxxxx.info/merchant/balance?password=$main_password)
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(req)
end
puts res.body
reference https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTP.html#class-Net::HTTP-label-HTTPS

Setting outgoing IP address in open-uri with RoR

I'm new to open-uri and trying to set an outgoing IP address using open-uri in ruby on rails. I used this post as a reference to get started. I'm porting an app from PHP where I could use CURLOPT_INTERFACE in curl_setopt. What's the best way to do this using open-uri in rails? (Doing this from the controller - not command line.)
If there's not a way to do this - any suggestions on an alternative to open-uri? My goal is to take in and parse JSON data.
What I understand from your questions is you want to hit another server from a specific IP which suggests you have a server with couple of addresses.
What I can suggest you is try to execute curl directly and do what you want to do or use a wrapper for it.
Doesn't look like open-uri can do this. But with net/https it's fairly easy.
require 'net/https'
require 'json'
uri = URI('https://jsonvat.com/')
http = Net::HTTP.new(uri.host, uri.port)
http.local_host = '1.2.3.4'
http.use_ssl = true
request = Net::HTTP::Get.new('/')
request.content_type = 'application/json'
request.initialize_http_header('Content-Type' => 'application/json')
response = http.request(request)
json = JSON.parse(response.body)
Probably you don't need the "require" lines inside Rails Controllers.
You can specify the outgoing IP address with the http.local_host line.
https://stackoverflow.com/a/24896074/1371731
https://yukimotopress.github.io/http

ruby http error - wrong Content-Length format

I have the following code
url = URI.parse("http://localhost:3100/tasks/#{id}.xml")
http = Net::HTTP.new(url.host, url.port)
# Make request
response = http.start do |http|
http.get(url.request_uri)
end
Whenever this code is run, it returns the following error
Net::HTTPHeaderSyntaxError in TasksController#show
wrong Content-Length format
Does anyone know whats going wrong here?
Thanks,
Josh
Hmm, this code does not look right:
response = http.start do |http|
The variable you are passing to your block should not have the same name as an existing variable. You may want something more like what is in the docs.
require 'net/http'
url = URI.parse('http://www.example.com/index.html')
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
puts res.body
Your code works when I point it to, for example, http://www.google.com, so I suspect that your server is spitting out something that gives Net::HTTP indigestion. Try adding this line just after your call to HTTP.new:
http.set_debug_output($stderr)
That should spew the entire transaction to the screen. I imagine you'll find something odd with the Content-Length field in the response header (Net::HTTP expects to find a string of digits in the that field). If it's not obvious from that what's the matter, then post that output (or at least the header portion of the response) here and let these smart guys have a go at it.

Resources