how do I include a header in an http request in ruby - ruby-on-rails

Have the below code working:
uri = URI.parse("http://www.ncdc.noaa.gov/cdo-web/api/v2/datasets/")
response = Net::HTTP.get_response(uri)
Now I also need to pass a header with this token hash in it:
token: "fjhKJFSDHKJHjfgsdfdsljh"
I cannot find any documentation on how to do this. How do I do it?

get_response is a shorthand for making a request, when you need more control - do a full request yourself.
There's an example in ruby standard library here:
uri = URI.parse("http://www.ncdc.noaa.gov/cdo-web/api/v2/datasets/")
req = Net::HTTP::Get.new(uri)
req['token'] = 'fjhKJFSDHKJHjfgsdfdsljh'
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}

Though you surely could use Net::HTTP for this goal, gem excon allows you to do it far easier:
require 'excon'
url = 'http://www.ncdc.noaa.gov/cdo-web/api/v2/datasets/'
Excon.get(url, headers: {token: 'fjhKJFSDHKJHjfgsdfdsljh'})

Related

Read a private file from Github in Ruby

Looked at this for initial solutions. However, the file I want to reference in my rails project is in a private file. When I perform the following code:
uri = URI("https://.../config.yml")
file = Net::HTTP.get(uri)
config = YAML.load(file)
The 'file' has the contents of the sign-in page of github. Is it possible to pass credentials to access this private repo's file? Additionally, is this safe to do?
You have to set the credentials via a header. Something like this should work
token = "123"
req = Net::HTTP::Get.new(uri)
req['Authorization'] = "Token #{token}"
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
Otherwise I also suggest to just use the Github client library Octokit.

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

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

Ruby on Rails HTTPS Post Bad Request

Greetings all.
My application works with a remote server. Server uses https
authorization of the certificate. I have following code to authorize and
sends request:
uri = URI.parse("https://db1-test.content.ertelecom.ru/")
http = Net::HTTP.new(uri.host, '443')
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.ca_file = File.join(File.dirname("public/certificate.pem"),
"certificate.pem")
http.start do |http|
req =
Net::HTTP::Get.new("/cgi-bin/expr/export.get_pay_systems?partner_id=1003")
responce = http.request(req)
resp = responce.body
end
this code works well, I get the data from the server. BUT when I try to
make POST request:
http.start do |http|
req =
Net::HTTP::Post.new("/cgi-bin/expr/payment_transactions.verify_order",
params)
responce = http.request(req)
resp = responce.body
end
I get an error from the server:
Your browser sent a request that this server could not understand.
Request header field is missing ':' separator.
what is that be? I tried to find a solution, but to no avail. the
Internet caught the message that it could be antivirus, but I'm on
Linux. I will be glad to any thoughts!
You're not filling the header data.
You could either use the Net::HTTP.post_form method to create your request or populate the form_data yourself.
post_form solution:
req = NET::HTTP.post_form("/cgi-bin/expr/payment_transactions.verify_order", params)
manual form_data population
req =
Net::HTTP::Post.new("/cgi-bin/expr/payment_transactions.verify_order")
req.set_form_data(params)

Rails: How to get the servers response HTTP headers?

I want to autodetect the pingback-url of remote websites, so I need to parse the HTTP response headers sent by the remote server. I don't need and don't want the contents of the remote website, I'm only looking for something like this:
X-Pingback: http://www.techcrunch.com/xmlrpc.php
Similar to using curl:
curl -I "your url"
Is there a way to do this with rails? When using open-uri I can only get the contents but not the headers.
Thank you!
Ole
This is not a Rails specific question, but it can be solved with the core Ruby API.
require 'net/http'
url = URI.parse('http://www.google.ca/')
req = Net::HTTP::Head.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
puts res['X-Pingback']
> "http://www.techcrunch.com/xmlrpc.php"
If you don't mind using a gem, you might try Patron, which is a ruby wrapper for the libcurl library. Usage might look something like this:
sess = Patron::Session.new
sess.timeout = 10
sess.base_url = "http://myserver.com:9900"
resp = sess.get( "your url" )
headers = resp.headers

Resources