Get HTTPS GET request with headers in Ruby - ruby-on-rails

I have the following curl command that works fine:
curl "https://api.multisafepay.com/v1/json/gateways" -X GET -H "api_key: e983177756a109e87aa5edbe05e0xxxxxxxxxxxx"
When I try to do the same from within Rails (I tried with HTTP and with Faraday I always get back a response that contains:
"error_info\": \"Invalid API key\"
This is the command I do for Faraday:
conn = Faraday.new 'https://api.multisafepay.com/v1/json/'
conn.headers = {'api_key' => 'e983177756a109e87aa5edbe05e0xxxxxxxxxxxx'}
conn.get 'gateways'
What is the difference with the Curl command that is giving a correct result set?
Edit: debugging into Faraday shows me that in this method:
def run_request(method, url, body, headers)
headers are nil? I'm continuing my search why this is.

I don't think you're giving Faraday the right URL. Try something like this
conn = Faraday.new do |f|
f.headers['api_token'] = <yourkey>
f.request :url_encoded
end
resp = conn.get('https://api.multisafepay.com/v1/json/gateways')

require 'rest-client'
require 'json'
response = RestClient.get("https://api.multisafepay.com/v1/json/gateways",
{'api_key' => 'e983177756a109e87aa5edbe05e0xxxxxxxxxxxx'}
)
puts JSON.parse(response, { symbolize_names: true })
#just in case try this one for header
#{:content_type => :json, :accept => :json, :api_key => 'e983177756a109e87aa5edbe05e0xxxxxxxxxxxx'}

Related

Net/HTTP in rails with request header and body

I am trying to call external API for my project and I have some troubles while using Net::HTTP in my rails lib . Here is my code
class ApiCall
def self.do_api_request(api_token, body)
require 'net/http'
require 'uri'
uri = URI.parse('https://sample.com')
header = {'Token' => api_token, 'Content-Type' => 'application/json', 'Accept' => 'application/json'}
request = Net::HTTP::Post.new(uri.request_uri, header)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == "https")
request.body = body
http.request(request)
end
end
This is how I use it (assume I knew the api_token and body):
body = {'id' => 2, 'age'=> 23};
ApiCall.do_api_request(api_token, body)
This way, it throws an error back:
NoMethodError: undefined method `bytesize' for Hash
Then after check online, seems like the body is hash instead of string, so I did this
body = URI.encode_www_form(body) and after rerun, it gives me :
400 bad request
I have no ideas how to put both header and body into a rails Net::HTTP method
Solution:
I know where the problem is. request body supposed to be string
so body = "{'id' : 2, 'age' : 23}" , I used body.to_json
I will suggest you to use HTTParty for calling an api. This is real simple to use. Following are the examples-
HTTParty.get("https://api.bigcommerce.com/stores/"+#store.store_hash+"/v3/catalog/categories", :headers => #your_header_data)
This will return the response. Also for post request,
HTTParty.post("https://api.bigcommerce.com/stores/"+#store.store_hash+"/v3/catalog/products", :headers => #auth, :body => product_json)
So you can pass body to in body param here.

Translating a curl command to httparty

I want to consume some API data from a Rails app. A curl example is curl --data 'api_key=your_api_key&api_secret=your_api_secret&host_id=your_user_host_id' https://api.zoom.us/v1/webinar/list I have experimented with this at the terminal and I am seeing expected responses. I’m now experimenting in a ruby script using httparty. My question is how should I handle the ‘stuff’ before the endpoint (api_key…secret…ect)? Are these headers?
In regard to curl --data only tells me that it is a post request, but I'm not sure how that translates to httparty.
Here is a first attempt:
require 'httparty'
api_key = 'myKey'
api_secret = 'secret'
host_id = 'host'
data_type = 'JSON'
response = HTTParty.post("api_key&api_secret&host_id&data_type https://api.zoom.us/v1/webinar/list/registration")
puts response.parsed_response
But this gives me a bad URI response. If I run this same script with the endpoint only I do get a response code back from zoom saying that API key and secret are required.
Looking at this example I think this should work:
require 'httparty'
api_key = 'myKey'
api_secret = 'secret'
host_id = 'host'
data_type = 'JSON'
options = {
body: {
api_key: api_key,
api_secret: api_secret,
host_id: host_id,
data_type: data_type
}
}
response = HTTParty.post("https://api.zoom.us/v1/webinar/list/registration", options)
puts response.parsed_response
I get the response:
{"error"=>{"code"=>200, "message"=>"Invalid api key or secret."}}
which I think is a step in the right direction.
No those are not headers those are parameters. Header are usually denoated by the -H flag.
Try this:
require 'httparty'
query_params = {api_key: 'myKey',
api_secret: 'secret',
host_id: 'host',
data_type: 'JSON'}
response = HTTParty.post("api_key&api_secret&host_id&data_type https://api.zoom.us/v1/webinar/list/registration", :query => query_params)
puts response.parsed_response

http Post request And declaration of authorization header in rails

I am trying to perform an HTTP authorization using Ruby on Rails. Here is what I'm trying:
res = http.post(uri.request_uri,
:Authorization => cobSessionToken,
"coBrandSessionCredential=loginToken=#{cobSessionToken}&userLogin=#{login}&userPassword=#{password}")
render :json => {"isValid" => true, "Body" => JSON.parse(res.body)}
This doesn't seem to work. How can I perform an authorization?
how about something like this?
url = URI.parse('https://my.url.com/path')
req = Net::HTTP::Post.new(url.path)
req.basic_auth 'user', 'pass'
req.use_ssl = true
req.form_data({'key1' => 'val1', 'key2' => 'val2'})
resp = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
puts resp
I would recommend using something like postman (its a free google program you can get at the google store) to make sure the error is not on the server side. Use Net:http it comes with ruby so you do not need to install it but you have to require it.
Require it by:
require "net/http"
require "uri"
Use this cheatsheet I think you need basic_auth.rb You will see how to form the request.

HTTParty and authorization via token

Somehow HTTParty returns 401 where CURL works fine. Not sure how to pass token in headers.
Working (200):
curl http://localhost:3020/api/products -H 'Authorization: Token token="111"'
Not working (401):
HTTParty.get('http://localhost:3020/api/products', headers: {"Authorization: Token token" => '111'})
I have tried with just "Authorization" => '111'and "token" => '111' but same result.
Managed to get it working as follows.
HTTParty.get("http://localhost:3020/api/products", headers: {"Authorization" => "Token token=\"111\""})
This also works if you want to set headers of the class dynamically, this example is for obtaining the Authorization token for Dun and Bradstreet
require 'httparty'
require 'certified'
class DnbAuth
include HTTParty
debug_output $stdout
base_uri "https://maxcvservices.dnb.com/rest/Authentication"
def initialize(ct,u,p)
self.class.headers 'Content-type' => "#{ct}"
self.class.headers 'x-dnb-user' => "#{u}"
self.class.headers 'x-dnb-pwd'=> "#{p}"
end
def token()
response = self.class.post("/")
end
end
ct = 'text/xml'
u = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
p = 'xxxxxx'
xx = DnbAuth.new(ct,u,p)
puts xx.token.message

check https status code ruby

Is there a way to check for an HTTPS status code in ruby? I know that there are ways to do this in HTTP using require 'net/http', but I'm looking for HTTPS. Maybe there is a different library that I need to use?
You can do this in net/http:
require "net/https"
require "uri"
uri = URI.parse("https://www.secure.com/")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
res = http.request(request)
res.code #=> "200"
Refs:
Net::HTTP cheat sheet
How to Cure Net::HTTP’s Risky Default HTTPS Behavior
You can use any wrapper around Net::HTTP(S) to get much easier behavior.
I use Faraday here ( https://github.com/lostisland/faraday ) but HTTParty has almost the same functionality ( https://github.com/jnunemaker/httparty )
require 'faraday'
res = Faraday.get("https://www.example.com/")
res.status # => 200
res = Faraday.get("http://www.example.com/")
res.status # => 200
(as a bonus you get options for parsing responses, raising state exceptions, logging requests....
connection = Faraday.new("https://www.example.com/") do |conn|
# url-encode the body if given as a hash
conn.request :url_encoded
# add an authorization header
conn.request :oauth2, 'TOKEN'
# use JSON to convert the response into a hash
conn.response :json, :content_type => /\bjson$/
# ...
conn.adapter Faraday.default_adapter
end
connection.get("/")
# GET https://www.example.com/some/path?query=string
connection.get("/some/path", :query => "string")
# POST, PUT, DELETE, PATCH....
connection.post("/some/other/path", :these => "fields", :will => "be converted to a request string in the body"}
# add any number of headers. in this example "Accept-Language: en-US"
connection.get("/some/path", nil, :accept_language => "en-US")
require 'uri'
require 'net/http'
res = Net::HTTP.get_response(URI('http://www.example.com/index.html'))
puts res.code # -> '200'
Slightly more readable way:
response.kind_of?(Net::HTTPOK)

Resources