How should i add header to HTTP GET request using ruby - ruby-on-rails

Trying to add header in the request
email = "blahblah#gmail.com"
token = "abcdefghijk"
url = "http://www.somewebsite.com/request?params1=value1&params2=value2"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port).start
request = Net::HTTP::Get.new(uri.request_uri)
request['email'] = email
request['token'] = token
response = http.request(request)
render json: response.body
The results i have gotten back is {"error":"invalid request parameters"}
I am supposed to get back a list of data in json. I tried using postman to test if the url is working and passed the email and token inside header and i got back the data that i wanted.
I am not sure where it went wrong with the code. Can anybody advise me which part did i do wrongly? Thanks a lot!

Try this.
email = "blahblah#gmail.com"
token = "abcdefghijk"
url = "http://www.somewebsite.com/request?params1=value1&params2=value2"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port).start
request = Net::HTTP::Get.new(uri.request_uri, {'email'=>email,'token'=>token})
response = http.request(request)
render json: response.body
OR
request = Net::HTTP::Get.new(uri.request_uri)
request.add_field("email", email)
request.add_field("token", token)
response = http.request(request)
render json: response.body

Related

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?

Https request wth user auth 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.

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)

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