Parsing HTTPS instead of HTTP in Rails - ruby-on-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

Related

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.

How can I force Rails to only use TLS over SSL?

We are being forced to only use TLS instead of SSL when sending a request to a server, but we don't know how to make sure that the request is being sent using TLS, and if it's not, we're not sure how to force Rails to do it.
Here is how we are sending the request:
uri = URI.parse("https://someurl.com")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.path)
request.body = body
http.request(request)
try to add
http.ssl_options = OpenSSL::SSL::OP_NO_SSLv2 + OpenSSL::SSL::OP_NO_SSLv3 + OpenSSL::SSL::OP_NO_COMPRESSION

Make a GET Request and receive parameters

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

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.

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)

Resources