Https request wth user auth rails - ruby-on-rails

curl -u "token:53555" https://url/method
how to send token in Net::Http url, I tried as
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri,{'token' => '53555'})
response = http.request(request)
But its not working

Try
uri = URI.parse(url + "?token=53555")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)

request['authorization'] = "Token token=#{token}" Set token like this.

You need to add basic_auth to your request object. Like so:
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request.basic_auth 'token', '53555'
response = http.request(request)
The request.basic_auth 'token', '53555' is the line that adds the basic authentication code.

Related

How to use certain curl in ruby rails

I have tried using curl-to-ruby (https://jhawthorn.github.io/curl-to-ruby/)
uri = URI.parse("https://api.wappalyzer.com/lookup/v1/?url=https://example.com")
request = Net::HTTP::Get.new(uri)
request["X-Api-Key"] = "wappalyzer.api.demo.key"
req_options = { use_ssl: uri.scheme == "https", }
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
Had no success, it gives back a http response code.
This is my curl:
curl -H "X-Api-Key: wappalyzer.api.demo.key" https://api.wappalyzer.com/lookup/v1/?url=https://example.com&callback_url=https://theverge.com
It's supposed to give back data about in what a website is made.
example of what its supposed to give:
[{"monthYear":"12-2019","languages":[],"applications":[{"name":"Apache","categories":["Web Servers"],"versions":["2.4.29"],"hits":754},{"name":"Ubuntu","categories":["Operating Systems"],"versions":[],"hits":754},{"name":"PHP","categories":["Programming Languages"],"versions":[],"hits":713},{"name":"Symfony",
Your code seems to work but you need to look at response.body:
require 'net/http'
uri = URI.parse("https://api.wappalyzer.com/lookup/v1/?url=https://example.com")
request = Net::HTTP::Get.new(uri)
request["X-Api-Key"] = "wappalyzer.api.demo.key"
req_options = { use_ssl: uri.scheme == "https" }
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
puts response.body

rails: User Net::Http post json to reply review google app develop api

I want to reply a review from google app.
This is link document: https://developers.google.com/android-publisher/api-ref/reviews/reply
I use Net::HTTP to post data.
my method to reply review
def self.reply_review(package , access_token, review_id, text )
uri = URI("https://www.googleapis.com/androidpublisher/v2/applications/#{package}/reviews/#{review_id}:reply?access_token=#{access_token}")
puts "https://www.googleapis.com/androidpublisher/v2/applications/#{package}/reviews/#{review_id}:reply?access_token=#{access_token}"
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
params = {
"replyText" => text
}
request = Net::HTTP::Post.new(
uri.request_uri,
'Content-Type' => 'application/json'
)
request.body = params.to_json
response = http.request(request)
puts response
puts(response.body)
response
end
but it always response
=> => #<Net::HTTPBadRequest 400 Bad Request readbody=true>
I am sure my data into accurate (packageName, access_token, replyId).
And how to fix this to reply a review use Net::HTTP
I have used this to connect with google Verification API.
require 'openssl'
require 'net/http'
uri =URI.parse("https://www.googleapis.com/androidpublisher/v2/applications/#{package}/reviews/#{review_id}:reply?access_token=#{access_token}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = Net::HTTP::Post.new(uri.request_uri,
initheader = {'Content-Type' =>'application/json'})
request.form_data = {"replyText" => text}
response = http.request(request)

Connection reset by peer - SSL_connect

Sometime i got this error when i send Https request from RAILS server and sometimes works.
I want to understand the reason behind not working and also how it's working sometimes.
My code:
require 'net/http'
require "uri"
require 'net/https'
def parse_query(classname, conditions)
url = "https://link"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
request["X-Parse-Application-Id"] = "some data"
request["X-Parse-REST-API-Key"] = "some data"
request["Content-Type"] = "application/json"
response = http.request(request)
end
Rails version = 4.2.1
Ruby version = 2.2.1

rails + satelizer - twitter login

I am using Satelizer for social logins in a rails app.
There's no ruby implementation, so I made one adapted from the node version.
So, after pressing the login with twitter button, I get to my controller, and the following code produces the message: "Bad Authentication data", code: 215
What am I doing wrong?
requestTokenUrl = 'https://api.twitter.com/oauth/request_token'
accessTokenUrl = 'https://api.twitter.com/oauth/access_token'
profileUrl = 'https://api.twitter.com/1.1/users/show.json?screen_name='
requestTokenOauth = {
consumer_key: ENV['TWITTER_KEY'],
consumer_secret: ENV['TWITTER_SECRET'],
callback: ENV['TWITTER_CALLBACK']
};
params = {'oauth' => requestTokenOauth }
require "net/https"
require "uri"
uri = URI.parse requestTokenUrl
http = Net::HTTP.new(uri.host, uri.port)
# https stuff
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(params)
response = http.request(request)
binding.pry

HTTP.post_form in Ruby with custom headers

Im trying to use Nets/HTTP to use POST and put in a custom user agent. I've typically used open-uri but it cant do POST can it?
I use
resp, data = Net::HTTP.post_form(url, query)
How would I change this to throw custom headers in?
Edit my query is:
query = {'a'=>'b'}
You can try this, for example:
http = Net::HTTP.new('domain.com', 80)
path = '/url'
data = 'form=data&more=values'
headers = {
'Cookie' => cookie,
'Content-Type' => 'application/x-www-form-urlencoded'
}
resp, data = http.post(path, data, headers)
You can't use post_form to do that, but you can do it like this:
uri = URI(url)
req = Net::HTTP::Post.new(uri.path)
req.set_form_data(query)
req['User-Agent'] = 'Some user agent'
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
case res
when Net::HTTPSuccess, Net::HTTPRedirection
# OK
else
res.value
end
(Read the net/http documentation for more info)
I needed to post json to a server with custom headers. Other solutions I looked at didn't work for me. Here was my solution that worked.
uri = URI.parse("http://sample.website.com/api/auth")
params = {'email' => 'someemail#email.com'}
headers = {
'Authorization'=>'foobar',
'Date'=>'Thu, 28 Apr 2016 15:55:01 MDT',
'Content-Type' =>'application/json',
'Accept'=>'application/json'}
http = Net::HTTP.new(uri.host, uri.port)
response = http.post(uri.path, params.to_json, headers)
output = response.body
puts output
Thanks due to Mike Ebert's tumblr:
http://mikeebert.tumblr.com/post/56891815151/posting-json-with-nethttp
require "net/http"
uri = URI.parse('https://your_url.com')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.ca_path='/etc/pki/tls/certs/'
http.ca_file='/etc/pki/tls/certs/YOUR_CERT_CHAIN_FILE'
http.cert = OpenSSL::X509::Certificate.new(File.read("YOUR_CERT)_FILE"))
http.key = OpenSSL::PKey::RSA.new(File.read("YOUR_KEY_FILE"))
#SSLv3 is cracked, and often not allowed
http.ssl_version = :TLSv1_2
#### This is IMPORTANT
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
#Crete the POST request
request = Net::HTTP::Post.new(uri.request_uri)
request.add_field 'X_REMOTE_USER', 'soap_remote_user'
request.add_field 'Accept', '*'
request.add_field 'SOAPAction', 'soap_action'
request.body = request_payload
#Get Response
response = http.request(request)
#Review Response
puts response.body

Resources