Error after trying to access xml (Zlib::BufError) (Ruby) - ruby

I keep getting and error whenever I try and access an XML file in Ruby. This is my code:
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
require 'open-uri'
url = 'http://access.alchemyapi.com/calls'
service = '/text/TextGetRankedTaxonomy'
apikey = '?apikey=4317fce9281094613deee9ebcc5aaf5238cd0748'
thething = '&text='
text = 'men%27s%20white%20crew%20neck%20shirt'
fullurl = url + service + apikey + thething + text
opener = open(fullurl) {|f| f.read }
Here is the error:
C:/Ruby21/lib/ruby/2.1.0/net/http/response.rb:357:in `finish': buffer error (Zlib::BufError)
from C:/Ruby21/lib/ruby/2.1.0/net/http/response.rb:357:in `finish'
from C:/Ruby21/lib/ruby/2.1.0/net/http/response.rb:262:in `ensure in inflater'
from C:/Ruby21/lib/ruby/2.1.0/net/http/response.rb:262:in `inflater'
from C:/Ruby21/lib/ruby/2.1.0/net/http/response.rb:274:in `read_body_0'
from C:/Ruby21/lib/ruby/2.1.0/net/http/response.rb:201:in `read_body'
from C:/Ruby21/lib/ruby/2.1.0/net/http/response.rb:226:in `body'
from C:/Ruby21/lib/ruby/2.1.0/net/http/response.rb:163:in `reading_body'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:1420:in `block in transport_request'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:1411:in `catch'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:1411:in `transport_request'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:1384:in `request'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:509:in `block in post_form'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:853:in `start'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:583:in `start'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:507:in `post_form'
from C:/Users/KVadher/Desktop/test151:11:in `<main>'
Is there anything I can do to either solve the error or run past it?

There is something wrong with the way this API server is encoding the data. To bypass this, you can simple say in the HTTP header that you don't accept any kind of encoding:
opener = open(fullurl, 'Accept-Encoding' => '') {|f| f.read }

Related

Ruby net/http can't connect to a TLS proxy

Rubys net/http allows you to specify a proxy to route all requests through like this:
Net::HTTP.new("host", "443", "proxyHost", 8080, "proxyUsername", "password")
This works fine when used with HTTP proxy however fails when a TLS proxy used instead of HTTP:
def tls_proxy(data)
uri = URI("https://example.com/post")
client = Net::HTTP.new("host", "443", "proxyHost", 8443, "proxyUsername", "password")
client.ssl_version = :TLSv1_2
client.use_ssl = true
request = Net::HTTP::Post.new(uri.path)
request['Content-type'] = 'application/json'
request.body = data
client.request(request)
end
Uncaught exception: end of file reached
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/protocol.rb:225:in `rbuf_fill'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/protocol.rb:191:in `readuntil'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/protocol.rb:201:in `readline'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http/response.rb:40:in `read_status_line'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http/response.rb:29:in `read_new'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http.rb:969:in `connect'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http.rb:930:in `do_start'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http.rb:919:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http.rb:1470:in `request'
/Users/laptop/Projects/tls-proxy-ruby/example-https.rb:47:in `tls_proxy'
/Users/laptop/Projects/tls-proxy-ruby/example-https.rb:60:in `<top (required)>'
Is there a way to make it work with TLS proxy or perhaps there is an alternative to this library that supports TLS proxy?
You could try httparty gem instead.
require 'httparty'
options = {
http_proxyaddr: '127.0.0.1',
http_proxyport: '8443',
http_proxyuser: 'johndoe',
http_proxypass: '123456'
}
response = HTTParty.get('https://example.com', options)

Faraday::Error: :hashie is not registered on Faraday::Middleware

I am creating a wrapper gem for a RESTful API. I am getting following error when I try to add hashie middleware as per the documentation:
Magpress::Login::#call#test_0001_should return valid JWT token:
Faraday::Error: :hashie is not registered on Faraday::Middleware
/home/amit/.rvm/gems/ruby-2.2.2#magpress/gems/faraday-0.10.0/lib/faraday.rb:184:in `lookup_middleware'
/home/amit/.rvm/gems/ruby-2.2.2#magpress/gems/faraday-0.10.0/lib/faraday/rack_builder.rb:204:in `use_symbol'
/home/amit/.rvm/gems/ruby-2.2.2#magpress/gems/faraday-0.10.0/lib/faraday/rack_builder.rb:84:in `use'
/home/amit/projects/bt/magpress/lib/magpress/client.rb:24:in `block in connection'
/home/amit/.rvm/gems/ruby-2.2.2#magpress/gems/faraday-0.10.0/lib/faraday/connection.rb:91:in `initialize'
/home/amit/.rvm/gems/ruby-2.2.2#magpress/gems/faraday-0.10.0/lib/faraday.rb:70:in `new'
/home/amit/.rvm/gems/ruby-2.2.2#magpress/gems/faraday-0.10.0/lib/faraday.rb:70:in `new'
/home/amit/projects/bt/magpress/lib/magpress/client.rb:18:in `connection'
/home/amit/projects/bt/magpress/lib/magpress/base.rb:7:in `initialize'
/home/amit/projects/bt/magpress/test/login_spec.rb:13:in `new'
/home/amit/projects/bt/magpress/test/login_spec.rb:13:in `block (3 levels) in <top (required)>'
Here is how I delcared dependencies in .gemspec.
...
spec.add_dependency "faraday"
spec.add_dependency "faraday_middleware"
spec.add_dependency "hashie"
....
and the class that uses faraday and it's middleware
require 'faraday'
require 'faraday_middleware'
require 'hashie'
module Magpress
class Client
def connection(url)
conn = ::Faraday.new(url) do |faraday|
faraday.request :json
faraday.response :json, :content_type => /\bjson$/
# faraday.use :instrumentation
faraday.use :hashie # FaradayMiddleware::Mashify
faraday.adapter Faraday.default_adapter
end
conn
end
end
end
farady and middlware gem versions
amit#amit:~/projects/bt/magpress$ gem list | grep faraday
faraday (0.10.0)
faraday_middleware (0.10.1)
What could be wrong here?
If I replace :hashie with FaradayMiddleware::Mashify, the errors goes away but the response.body returns instance of vanilla Hash instead of
Hashie::Mash
Voila! It is fixed.
Current
faraday.response :json, :content_type !=> /\bjson$/
faraday.use :hashie # FaradayMiddleware::Mashify
After Fix
faraday.response :mashify
faraday.response :json, :content_type => /\bjson$/
I could fix by reading the code!

Ruby: How to access an api using HTTParty

I'm new to Ruby, and to using HTTParty, and was trying to follow the HTTParty examples from their github page to execute a basic POST. When I run the code below I get an error:
require 'pp'
require 'HTTParty'
require 'pry'
class Partay
include HTTParty
base_uri "http://<myapidomain>/search/semanticsearch/query/"
end
options= {
query: {
version: "0.4",
query: "lawyer"
}}
response = Partay.post(options)
puts response
The error I get is:
rbenv/versions/2.2.0/lib/ruby/2.2.0/uri/common.rb:715:in `URI': bad argument (expected URI object or URI string) (ArgumentError)
from ~/.ruby/2.2.0/gems/httparty-0.13.3/lib/httparty/request.rb:47:in `path='
from ~/.ruby/2.2.0/gems/httparty-0.13.3/lib/httparty/request.rb:34:in `initialize'
from ~/.ruby/2.2.0/gems/httparty-0.13.3/lib/HTTParty.rb:539:in `new'
from ~/.ruby/2.2.0/gems/httparty-0.13.3/lib/HTTParty.rb:539:in `perform_request'
from ~/.ruby/2.2.0/gems/httparty-0.13.3/lib/HTTParty.rb:491:in `post'
from json-to-csv.rb:16:in `<main>'
What I am looking for is calling a post that receives JSON in the same way that calling this URL works:
http://somedomain.com/search/semanticsearch/query/?version=0.4&query=lawyer
Noting a solution with the suggested gem - unirest:
require 'unirest'
url = "http://somedomain.com/search/semanticsearch/query"
response = Unirest.post url,
headers:{ "Accept" => "application/json" },
parameters:{ :version => 0.4, :query => "lawyer" }

ruby oauth2 put request returns invalid block type error

I'm able to perform get and post requests fine, but when I try to do a put request with the same format, I get:
Zlib::DataError (invalid block type)
here is the backtrace
/Users/psi/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http/response.rb:357:in `finish': invalid block type (Zlib::DataError)
from /Users/psi/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http/response.rb:357:in `finish'
from /Users/psi/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http/response.rb:262:in `ensure in inflater'
from /Users/psi/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http/response.rb:262:in `inflater'
from /Users/psi/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http/response.rb:274:in `read_body_0'
from /Users/psi/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http/response.rb:201:in `read_body'
from /Users/psi/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http/response.rb:226:in `body'
from /Users/psi/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http/response.rb:163:in `reading_body'
from /Users/psi/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http.rb:1412:in `block in transport_request'
from /Users/psi/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http.rb:1403:in `catch'
from /Users/psi/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http.rb:1403:in `transport_request'
from /Users/psi/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http.rb:1376:in `request'
from /Users/psi/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http.rb:1369:in `block in request'
from /Users/psi/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http.rb:852:in `start'
from /Users/psi/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http.rb:1367:in `request'
from /Users/psi/.rvm/gems/ruby-2.0.0-p451/gems/faraday-0.9.0/lib/faraday/adapter/net_http.rb:80:in `perform_request'
from /Users/psi/.rvm/gems/ruby-2.0.0-p451/gems/faraday-0.9.0/lib/faraday/adapter/net_http.rb:39:in `call'
from /Users/psi/.rvm/gems/ruby-2.0.0-p451/gems/faraday-0.9.0/lib/faraday/request/url_encoded.rb:15:in `call'
from /Users/psi/.rvm/gems/ruby-2.0.0-p451/gems/faraday-0.9.0/lib/faraday/rack_builder.rb:139:in `build_response'
from /Users/psi/.rvm/gems/ruby-2.0.0-p451/gems/faraday-0.9.0/lib/faraday/connection.rb:377:in `run_request'
from /Users/psi/.rvm/gems/ruby-2.0.0-p451/gems/oauth2-0.9.3/lib/oauth2/client.rb:90:in `request'
from /Users/psi/.rvm/gems/ruby-2.0.0-p451/gems/oauth2-0.9.3/lib/oauth2/access_token.rb:107:in `request'
from /Users/psi/.rvm/gems/ruby-2.0.0-p451/gems/oauth2-0.9.3/lib/oauth2/access_token.rb:128:in `put'
from api_check.rb:50:in `<main>'
here is the setup code
require 'oauth2'
client = OAuth2::Client.new(CLIENTID, CLIENTSECRET,
site: SITE, token_url: TOKENURL)
token = client.client_credentials.get_token
response = token.put(PATH, {body: {params1: PARAM1}})
Im using oauth2 version 0.9.3
If there is something wrong with the way I'm doing a request, I have no indication from the error log.
If more info is needed let me know.
If I use a different Faraday adapter like EventMachine, I get the correct OAuth2::Error exception with a detailed message instead of just breaking.
To change the adapter, add the desired adapter to the Gemfile and add this line before calling OAuth2 methods
Faraday.default_adapter = :em_http

Error while using Net::HTTP GET request

I am new to ruby . I have the following Code snippet which performs a GET operation and retrieves the names of my facebook groups
def get_groups
query=("SELECT gid,name FROM group where gid in(SELECT gid from group_member where uid=me)")
uri=URI("https://graph.facebook.com/fql")
params={'q'=>query,'access_token'=>TOKEN}
uri.query=URI.encode_www_form(params)
response=Net::HTTP.get_response(uri)
result=json.loads(response.text)
return result['data']
end
But when I execute the above code I get the following errors:
/usr/lib/ruby/1.9.1/net/protocol.rb:141:in `read_nonblock': Connection reset by peer (Errno::ECONNRESET)
from /usr/lib/ruby/1.9.1/net/protocol.rb:141:in `rbuf_fill'
from /usr/lib/ruby/1.9.1/net/protocol.rb:122:in `readuntil'
from /usr/lib/ruby/1.9.1/net/protocol.rb:132:in `readline'
from /usr/lib/ruby/1.9.1/net/http.rb:2562:in `read_status_line'
from /usr/lib/ruby/1.9.1/net/http.rb:2551:in `read_new'
from /usr/lib/ruby/1.9.1/net/http.rb:1319:in `block in transport_request'
from /usr/lib/ruby/1.9.1/net/http.rb:1316:in `catch'
from /usr/lib/ruby/1.9.1/net/http.rb:1316:in `transport_request'
from /usr/lib/ruby/1.9.1/net/http.rb:1293:in `request'
from /usr/lib/ruby/1.9.1/net/http.rb:1195:in `request_get'
from /usr/lib/ruby/1.9.1/net/http.rb:455:in `block in get_response'
from /usr/lib/ruby/1.9.1/net/http.rb:745:in `start'
from /usr/lib/ruby/1.9.1/net/http.rb:454:in `get_response'
from fb.rb:12:in `get_groups'
from fb.rb:32:in `<main>'
What is the mistake that I am making here?
I believe the issue here is you're using an HTTP library to access an HTTPS service. These are fundamentally different things. Here's an HTTPS example:
require 'net/http'
require 'net/https'
http = Net::HTTP.new('www.example.com', 443)
http.use_ssl = true
http.ssl_version = :TLSv1
http.ciphers = "ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:-LOW"
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
req = Net::HTTP::Get.new('/', {'Content-Type' =>'application/json'})
http.start {|http| http.request(req) }
It seems like you have SSL certificate validation issue. Try to turn off SSL validation.
Net::HTTP.verify_mode = OpenSSL::SSL::VERIFY_NONE
If above helps you, possible root of the issue is current OpenSSL installation. Try to update it to the latest version.

Resources