New York Public Library search query - ruby

I am attempting to access the NY Public Library digital collection through their api.
The ruby code I am using to make the call is:
require 'sinatra'
require 'pp'
require 'httparty'
get '/' do
url = "http://api.repo.nypl.org/api/v1/items/8568ccd0-c614-012f-1d74-58d385a7bc34.json"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
headers = { "Authorization" => "Token token=mytoken" }
request = Net::HTTP::Get.new(uri.request_uri, headers)
response = http.request(request)
puts response
#response = response.body
erb :index
end
So, a couple of questions:
This produces no result, nothing comes back with the puts response in the terminal, not even an error message. What am I doing wrong?
In any case, how do I attach a query at the end of the uri string? BY adding a ? followed by the query? So, for example, ?leonardodavinci

Recommend you play around at the irb command prompt, e.g.
irb [enter]
Looks like not authorized perhaps? I eventually get => nil myself with:
irb(main):003:0> require 'sinatra'
=> true
irb(main):004:0> require 'pp'
=> true
irb(main):005:0> require 'httparty'
=> false
irb(main):006:0> url = "http://api.repo.nypl.org/api/v1/items/8568ccd0-c614-012f-1d74-58d385a7bc34.json"
=> "http://api.repo.nypl.org/api/v1/items/8568ccd0-c614-012f-1d74-58d385a7bc34.json"
irb(main):007:0> uri = URI.parse(url)
=> #<URI::HTTP:0x9826744 URL:http://api.repo.nypl.org/api/v1/items/8568ccd0-c614-012f-1d74-58d385a7bc34.json>
irb(main):008:0> http = Net::HTTP.new(uri.host, uri.port)
=> #<Net::HTTP api.repo.nypl.org:80 open=false>
irb(main):009:0> headers = { "Authorization" => "Token token=mytoken" }
=> {"Authorization"=>"Token token=mytoken"}
irb(main):010:0> request = Net::HTTP::Get.new(uri.request_uri, headers)
=> #<Net::HTTP::Get GET>
irb(main):011:0> response = http.request(request)
=> #<Net::HTTPUnauthorized 401 Authorization Required readbody=true>
irb(main):012:0> puts response
#<Net::HTTPUnauthorized:0x9938a74>
=> nil
irb(main):013:0>

Related

Getting Headers from a Ruby Net::HTTP Request before making the request

In Ruby, how can I get hold of the HTTP Request Headers that will be sent by a net/http(s) or open-uri request BEFORE it actually makes the request.
In some cases, headers are used when creating a signed string in a URI. Surely there is some way to acquire the request headers that will be sent. These should include the "Host:" header for example.
see http://ruby-doc.org/stdlib-2.0/libdoc/net/http/rdoc/Net/HTTP.html#label-Setting+Headers
Works well in ruby 2.0.0 - but you are correct, different behavior in 1.9.3
Ruby 2.0.0
require 'net/http'
uri = URI('http://github.com/ruby')
http_request = Net::HTTP::Get.new(uri)
http_request.each_header { |header| puts header }
# => accept-encoding
# => accept
# => user-agent
# => host
http_response = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(http_request)
end
Ruby 1.9.3
require 'uri'
require 'net/http'
uri = URI.parse('http://github.com/ruby')
http_request = Net::HTTP::Get.new(uri.path)
http_request.each_header { |header| puts header }
# => accept
# => user-agent
http_response = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(http_request)
end
http_request.each_header { |header| puts header }
# => accept
# => user-agent
# => host
The Net::HTTP classes all seem to use Net::HTTPHeader as a mixin. You should be able to use to_hash() on the request object to get all headers at once, or each_header() / each() to iterate one header at a time.
Here's some code I wrote to help.
def get_request_headers(request)
http_method = request.method
path = request.path
request.to_hash.merge("method" => [http_method]).merge("path" => [path])
end
So now, you can run something like this.
url = URI("http://www.google.com")
request, response = Net::HTTP.start(uri(ico).host) do |http|
request = Net::HTTP::Get.new(uri(ico))
response = http.request request
[request, response]
end
get_request_headers(request)
=> {"accept-encoding"=>["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"], "accept"=>["*/*"], "user-agent"=>["Ruby"], "host"=>["www.google.com"], "method"=>["GET"], "path"=>["/"]}
request.to_hash give us a few headers for free, but there's more information stored in the instance variables for the request and response classes.
With the following code, you can check if there's anything else you'd like to merge into the basic request hash.
request.instance_variables.each do |variable|
puts "#{variable}: #{request.instance_variable_get(variable)}"
end
=> #method: GET
=> #request_has_body: false
=> #response_has_body: true
=> #uri: http://www.google.com
=> #path: /
=> #decode_content: true
=> #header: {"accept-encoding"=>["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"], "accept"=>["*/*"], "user-agent"=>["Ruby"], "host"=>["www.google.com"]}
=> #body:
=> #body_stream:
=> #body_data:
=> [:#method, :#request_has_body, :#response_has_body, :#uri, :#path, :#decode_content, :#header, :#body, :#body_stream, :#body_data]
Note that I've pulled out the method and path for the get_request_headers method.
Finally, you can do the same for the response.
def get_response_headers(response)
code = response.code
http_version = response.http_version
message = response.message
response.to_hash.merge("code" => [code]).merge("http_version" => [http_version]).merge("message" => [message])
end
get_response_headers(response)
=> {"date"=>["Thu, 06 May 2021 14:34:27 GMT"], "expires"=>["-1"], "cache-control"=>["private, max-age=0"], "content-type"=>["text/html; charset=ISO-8859-1"], "p3p"=>["CP=\"This is not a P3P policy! See g.co/p3phelp for more info.\""], "server"=>["gws"], "content-length"=>["6067"], "x-xss-protection"=>["0"], "x-frame-options"=>["SAMEORIGIN"], "set-cookie"=>["NID=215=dYowhmNSD9_CnKYLtsFI3uWVGy8ca8PKJTE8VY6_92q7tU5Y_AOWLsaabXxlSPBjc2QjOr4xXVX5SGMCrccTCnBR9pbdsKkrpVTV5TMqrV6H09ChxGjBr5mHVdZkgjOxswiXu72TF3eAX0uhXqloDb-5gmZ6NJ4w1YDKQKNoDp4; expires=Fri, 05-Nov-2021 14:34:27 GMT; path=/; domain=.google.com; HttpOnly"], "code"=>["200"], "http_version"=>["1.1"], "message"=>["OK"]}

unable to get the XML response using 'Get' request through 'Ruby' language

I am not able to get the xml response after triggering 'GET' request using Ruby language.My Current Code is as follows:
require 'net/https'
require 'uri'
require 'base64'
base_url = '<URL>'
app_guid = '<App GUID Value>'
format = "xml"
# Example using bug.fetch
params = {
"appGUID" => app_guid,
"format" => format,
"method" => "bug.fetch",
"bugId" => "1234567"
}
puts "XML Response"
res = Net::HTTP.post_form(URI.parse(base_url), params)
puts res.body
As Frederick notes, your code is making a POST request. If you want to use GET do:
uri = URI.parse(base_url)
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)
If you still encounter errors, you may wish to use this alternative syntax:
uri = URI.parse(base_url)
uri.query = URI.encode_www_form(params)
conn = Net::HTTP.new(uri.host, uri.port)
conn.use_ssl = true
res = conn.get uri.request_uri

Creating Gist from a Ruby script

Is there a way to create an Anonymous (public or private) gist using net/http or net/https?
This worked for me.
require 'net/http'
require 'json'
uri = URI("https://api.github.com/gists")
payload = {
'description' => "My test gist",
'public' => true,
'files' => {
'test.txt' => {
'content' => "This is a test!\n\nI am making a public gist."
}
}
}
req = Net::HTTP::Post.new(uri.path)
req.body = payload.to_json
puts req.inspect
puts req.body.inspect
# GitHub API is strictly via HTTPS, so SSL is mandatory
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http|
http.request(req)
end
puts res.inspect
puts res.body.inspect
Result: My test gist
This gem will do the trick for you https://github.com/defunkt/gist!
For the record, it does require net/https.

How to post json data with Rest API

require 'net/http'
require 'uri'
postData = Net::HTTP.post_form(URI.parse('http://localhost/restapi/index.php/api/posts'),
{'id'=>9,'firstname'=>"test","lastname"=>"test"})
puts postData.body
How can I send data in JSON form?
#toSend = {"id" =>5,"firstname" => "anurag","lastname" => "arya"}
I also tried this but it did not work:
#toSend.to_json
Example:
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
url = "http://localhost/restapi/index.php/api/posts"
uri = URI.parse(url)
data = {"id"=>11,
"firstname"=>"PWD","lastname"=>"last"}
headers = {'Content-Type' =>'application/json',
'Accept-Encoding'=> "gzip,deflate",
'Accept' => "application/json"}
http = Net::HTTP.new(uri.host,uri.port) # Creates a http object
#http.use_ssl = true # When using https
#http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.post(uri.path,data.to_json,headers)
puts response.code
puts response.body
postData=Net::HTTP.post_form(URI.parse('http://localhost/oecprashant/yiiIndex.php/api/rubyREST'),
{'data'=>jsonData})

RUBY - SSL, Basic Auth, and POST

I'm having quite a hard time with this -- seems like there are a few snippets of code lying around that I can't seem to piece together. I'm simply trying to POST key/value pairs, but getting Connection refused - connect(2) (Errno::ECONNREFUSED). Help!
require 'net/http'
require 'net/https'
require 'uri'
#http = Net::HTTP.new('https://my.url.com/path', 443)
#http.use_ssl = true
#http.start() { |http|
req = Net::HTTP.post_form(
URI.parse('https:///my.url.com/path'),
{'key1' => 'value1', 'key2' => 'value2'}
)
req.basic_auth 'username', 'password'
response = http.request(req)
puts response.body
}
HTTP#post_form will execute directly, ignoring your other settings. Try this instead:
require 'net/http'
require 'uri'
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
You are likely to run into trouble with the server's certificates. See my other post for instructions on how to get/configure them.
Found this question during my search for a solultion, and just to be clear emboss' code as is is non functional. What I got working is this (inside of a larger Sinatra application):
require 'net/http'
require 'net/https'
require 'uri'
set :api_username, 'usr'
set :api_passwor, 'pswd'
def do_auth_check params
puts params.inspect
url = URI.parse("https://my_auth_site.com:443/authenticate")
req = Net::HTTP::Post.new(url.path)
req.basic_auth options.api_username, options.api_password
req.set_form_data({'username' => params[:name], 'password' => params[:pass]})
sock = Net::HTTP.new(url.host, url.port)
sock.use_ssl = true
res = sock.start {|http| http.request(req) }
# you're on your own down here to identify success/failure, but for me 2xx/3xx was ok and 401/404/500/etc would be failure
return true if res.code.to_i < 400
return "Error logging in"
end

Resources