HTTP request to send sms from rails app - ruby-on-rails

I'm writing an sms sending function but it doesn't seem to send the sms.
Any idea by sight of this code why?
def self.send_sms(to, from, message)
username = "uname"
password = "pword"
id = rand(36**8).to_s(36)
dlr_url = URI::escape('http://www.skylinesms.com/messages/delivery?id=#{id}&type=%d')
send_url = 'http://localhost:13013/cgi-bin/sendsms?username=#{username}&password=#{password}&to=#{to}&from=#{from}&text=#{message}&dlr-url=#{dlr_url}&dlr-mask=3'
url = URI.parse(URI.encode(send_url))
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) }
return res.body
end

I'm not sure but I think so
def self.send_sms(to, from, message)
.....
Net::HTTP.start(url.host, url.port) { |http| http.get(req.request_uri).body }
end

Related

GitHub request get a repository

How can I make a get request to a repository if my user hasn't generated a personal token and I have just the access token from authentication with oauth2?
I have tried some options with postman but I can't fix it.
I want to access a private repository
What I'm trying to do:
require "uri"
require "net/http"
url = URI("https://api.github.com/repos/username/repository_name")
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Get.new(url)
request["Authorization"] = "Bearer ACCESS_TOKEN"
request["Cookie"] = "_octo=GH1.1.1832634711.1663350372; logged_in=no"
response = https.request(request)
puts response.read_body
One of my collaborators resolve in this way, I hope to be helpful to someone else
def get_github_link
#group=Group.find(params[:id])
if current_user.gh_access_token.nil?
flash[:notice] = "You havn't your GithHub account linked, this is a private repository!"
redirect_to #group and return
end
if #group.status=="private"
url = URI.parse("https://api.github.com/repos/#{current_user.gh_username}/#{#group.git_repository}")
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = "token #{current_user.gh_access_token}"
request["Content-Type"] = 'application/json'
request["Accept"] = 'application/vnd.github+json'
request["Coockies"] = 'login-yes'
request.body = {owner: #group.user.gh_username}.to_json
response = https.request(request)
else
url = URI.parse("https://api.github.com/repos/#{#group.user.gh_username}/#{#group.git_repository}")
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Get.new(url)
request["Authorization"] = "token #{current_user.gh_access_token}"
request["Accept"] = 'application/vnd.github+json'
request.body = {owner: #group.user.gh_username}.to_json
response = https.request(request)
end
#url = URI.parse("https://api.github.com/repos/"+#group.git_url.remove("https://github.com/"))
json = JSON.parse(response.body, symbolize_names: true)
if eval(response.code.to_s) === 200
redirect_to #group.git_url, allow_other_host: true and return
else
flash[:notice] = "You have not the access to this repository, please conctact the admin"
redirect_to #group and return
end
end

Put request using Net :: HTTP returns status 200 but does not work correctly

I created a method to change a subscription on an online platform through the API made available. The request returns status 200, but the change does not appear on the platform.
The method is as follows:
def self.update_subscription_item_value(subscription_id, item_id, value)
url = URI("https://api.iugu.com/v1/subscriptions/#{subscription_id}")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Put.new(url)
request['authorization'] = 'Basic ' + Base64.encode64(Iugu.api_key + ':').chomp
request.body = "{\"subitems\":[{\"id\":\"#{item_id}\",\"quantity\":\"#{value}\"}]}"
response = http.request(request)
puts response.read_body
end
request return: Net::HTTPOK 200 OK readbody=true
does anyone know why I didn't get any changes to the platform?

Error making http requests. Timeout :: Error and IOError: use_ssl value changed, but session already started

I have a weird problem that i have a feeling is just a tiny obvious error.
when i make the following implementation ;
url = URI.parse(helper.full_url)
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) { |http|
http.request(req)
}
Gives me there error below;
Timeout :: Error
But the this implementation;
url = URI.parse(helper.full_url)
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) { |http|
http.use_ssl = true
http.request(req)
}
gives me this error.
IOError: use_ssl value changed, but session already started
What could be the problem?
You cannot use http.use_ssl after Net::HTTP.start, you should use it before. Look at http://www.ruby-doc.org/stdlib-1.9.2/libdoc/net/http/rdoc/Net/HTTP.html#method-i-use_ssl-3D
# File net/http.rb, line 591
def use_ssl=(flag)
flag = (flag ? true : false)
if started? and #use_ssl != flag
raise IOError, "use_ssl value changed, but session already started"
end
#use_ssl = flag
end
try this:
uri = URI.parse(helper.full_url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'
http.start do
request = Net::HTTP::Get.new(uri.request_uri)
puts http.request(request)
end

Sending a Post request with net/http

I need to send data in JSON to another app which runs on the same computer.
I send request like so (rails 3.2.13 )
data = { //some data hash }
url = URI.parse('http://localhost:6379/api/plans')
resp, data = Net::HTTP.post_form(url, data.to_JSON )
p resp
p data
{ resp: resp, data: data.to_JSON }
But i get Net::HTTPBadResponse (wrong status line: "-ERR unknown command 'POST'"):
How can i solve this problem?
Update 1
Updated my code as #Raja-d suggested
url = URI.parse('http://localhost:6379/v1/sessions')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
resp, data = Net::HTTP.post_form(url, data)
p resp
p data
But i still get error Net::HTTPBadResponse (wrong status line: "-ERR unknown command 'POST'"):
I don't know what your problem is but what about something like this
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.path, {'Content-Type' => 'application/json'})
request.body = data.to_json
response = http.request(request)

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