How to use awesome_print for Ruby file that makes a HTTP request - ruby

This is my code:
require 'net/https'
uri = URI('https://api.clever.com/v1.1/sections')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = Net::HTTP::Get.new(uri.request_uri)
request.add_field 'Authorization', 'Bearer DEMO_TOKEN'
response = http.request(request)
puts response.body
The problem is that my code output is gross and hard to read in terminal. I'm trying to clean it up with awesome print but it isn't working... this is what I'm trying:
require 'net/https'
require 'awesome_print'
uri = URI('https://api.clever.com/v1.1/sections')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = Net::HTTP::Get.new(uri.request_uri)
request.add_field 'Authorization', 'Bearer DEMO_TOKEN'
response = http.request(request)
ap response.body
but it's not formatting at all the way I need it to. Any idea what's going on?

The problem is that you need to print the Hash, not the raw String. So use JSON.parse(response.body) would solve your problem.
Alternatively, use pp and json, they are all from stdlib.
require 'net/https'
require 'pp'
require 'json'
uri = URI('https://api.clever.com/v1.1/sections')
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
request = Net::HTTP::Get.new(uri)
request["authorization"] = "Bearer DEMO_TOKEN"
http.request(request) do |response|
pp JSON.parse(response.body)
end
end
But ultimately, I would recommend to use pry for debugging. It just make life easier 10x for debuging.
gem install pry
Then change above code to:
require 'net/https'
require 'pry'
require 'json'
uri = URI('https://api.clever.com/v1.1/sections')
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
request = Net::HTTP::Get.new(uri)
request["authorization"] = "Bearer DEMO_TOKEN"
http.request(request) do |response|
res = JSON.parse(response.body)
binding.pry
end
end
After running the file in your terminal, it will paused at where you put binding.pry. Then type res, you will see the nicely formatted hash.
Have fun with pry!

parse your response.body to JSON and use pretty_generate() function, built into later versions of JSON.
require 'net/https'
require 'json'
uri = URI('https://api.clever.com/v1.1/sections')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = Net::HTTP::Get.new(uri.request_uri)
request.add_field 'Authorization', 'Bearer DEMO_TOKEN'
response = http.request(request)
myjson = JSON.parse(response.body)
puts JSON.pretty_generate(myjson)
Which will give you output:
{
"data": {
"course_name": "Fine Arts, Class 703",
"course_number": "703",
"created": "2014-02-26T21:15:38.324Z",
"district": "4fd43cc56d11340000000005",
"grade": "7",
"last_modified": "2015-09-30T21:08:09.877Z",
"name": "Fine Arts, Class 703 - 703 - A. Ortiz (Section 3)",
"period": "7",
"school": "530e595026403103360ff9ff",
"sis_id": "674",
"students": [
"530e5960049e75a9262cff59",
"530e5960049e75a9262cff99",
"530e5961049e75a9262cffd5",
"530e5961049e75a9262d001c",
"530e5961049e75a9262d008a",
"530e5962049e75a9262d0144",
"530e5962049e75a9262d0155",
"530e5962049e75a9262d015e",
"530e5963049e75a9262d0200",
"530e5963049e75a9262d022d",
"530e5963049e75a9262d023a",
"530e5964049e75a9262d0275",
"530e5964049e75a9262d029b",
"530e5964049e75a9262d02c0",
"530e5964049e75a9262d02de",
"530e5965049e75a9262d034a",
"530e5965049e75a9262d0354",
"530e5965049e75a9262d03c7",
"530e5966049e75a9262d0419",
"530e5966049e75a9262d046d",
"530e5966049e75a9262d0489",
"530e5967049e75a9262d0560",
"530e5967049e75a9262d05b4",
"530e5967049e75a9262d05bb",
"530e5968049e75a9262d0621",
"530e5968049e75a9262d0637"
],
"subject": "arts and music",
"teacher": "530e5955d50c310f36112bec",
....
....
# I have not post full output but it's pretty good and well structured

awesome print is print your Ruby data structures (Hash, Array etc.) in an easy to read format. Not for HTML!
If you want to format HTML in an easy to read manner, take a look at Nokogiri. Example:
require 'nokogiri'
# your response html
html = response.body
doc = Nokogiri::XML(html,&:noblanks)
puts doc.to_xhtml(indent:4)

Related

'JSON.parse' error handling invalid/non JSON format

I request information from an HTTP streaming service. It provides data in JSON format. Here is the documentation. Here is a part of the code I am using:
require 'uri'
require 'net/https'
require 'json'
uri = URI("https://api.tradier.com/v1/markets/events/session")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
http.read_timeout = 30
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
# Headers
request["Accept"] = "application/json"
request["Authorization"] = "Bearer xxx"
# Send synchronously
response = http.request(request)
# parses response
parse = JSON.parse(response.body)
#out puts values only from response
sessionid = parse.values[0]["sessionid"]
url = parse.values[0]["url"]
uri = URI("#{url}?sessionid=#{sessionid}&symbols=aapl")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
http.read_timeout = 30
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
# Headers
request["Accept"] = "application/json"
request["Authorization"] = "Bearer xxx"
http.request request do |response|
response.read_body do |data|
puts data.class
# info = JSON.parse(data, :quirks_mode => true)
# puts info.values
end
end
I want to have the system continue the program. It seems that I need to use begin and rescue, but I cannot get them to work. When I request data, I get the following error:
`parse': 757: unexpected token at '{"type":"trade","symbol":"AAPL","exch":"Q","price":"191.23","size":"1081622","cvol":"18308460","date":"1528747200000","last":"191.23"}{"type":"summary","symbol":"AAPL","open":"191.35","high":"191.97","low":"190.21","prevClose":"191.7","close":"191.23"}' (JSON::ParserError)
The endpoint you are using is documented in Tradier API docs and it's a streaming endpoint.
It appears that the response is not chunked to contain just one JSON document per chunk. It however does appear that the documents are separated by linefeeds, making the response look like:
{ "json": "data" }
{ "more": "data" }
And that is not valid JSON. You probably need to parse them one by one by doing something like:
http.request request do |response|
response.read_body do |data|
data.each_line do |chunk|
info = JSON.parse(chunk)
puts info.inspect
end
end
end
If the response chunking happens in the middle of JSON documents, you must use some kind of buffered reader.

How to turn curl request with user and password into ruby NET::HTTP for https site?

I have a ruby script that I'm using to get info from a web page and update the page. I am getting some json info from the web page with:
`curl -s -u #{username}:#{password} #{HTTPS_PAGE_URL}`
And then I am updating the page with:
`curl -s -u #{username}:#{password} -X PUT -H 'Content-Type: application/json' -d'#{new_page_json_info}' #{HTTPS_PAGE_URL}`
I want to use Net::HTTP to do this instead. How can I do this?
For reference here is the confluence doc that I used to create the curl command in the first place: https://developer.atlassian.com/confdev/confluence-server-rest-api/confluence-rest-api-examples
can try doing something like:
uri = URI.parse("http://google.com/")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request.basic_auth("username", "password")
Thank you http://jhawthorn.github.io/curl-to-ruby
That solved it. All you have to do is give that website your curl command and it will convert it into a ruby script.
For the first curl (this gets the json info from a page and sends it to stdout):
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
uri = URI.parse("https://my.page.io/rest/api/content/")
request = Net::HTTP::Get.new(uri)
request.basic_auth("username", "password")
req_options = {
use_ssl: uri.scheme == "https",
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
# response.code
puts JSON.parse(response.body).to_hash
For the second (this updates the json info on a page):
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
require 'json'
uri = URI.parse("https://my.page.io/rest/api/content/")
request = Net::HTTP::Put.new(uri)
request.basic_auth("username", "password")
request.content_type = "application/json"
request.body = "{Test:blah}"
req_options = {
use_ssl: uri.scheme == "https",
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end

Posting Ruby data in JSON format with Net/http

I have this ruby file:
require 'net/http'
require 'json'
require 'uri'
#test data
newAcctJson ='{
"type": "Credit Card",
"nickname": "MoreTesting",
"rewards": 2,
"balance": 50
}'
#creates a new account
def createAcct(custID, json)
url = "http://api.reimaginebanking.com:80/customers/#{custID}/accounts?key=#{APIkey}"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
myHash = JSON.parse(json)
resp = Net::HTTP.post_form(uri, myHash)
puts(resp.body)
end
which attempts to create a new account. However I get code: 400, invalid fields in account. I tested the data independently, so I'm (relatively) certain that the json itself isn't in an incorrect format; the problem is in trying to submit the data in the hash format that the post_Form requires. Does anyone know a way to use ruby to directly post json data without converting to a hash first?
Make a request object like so:
request = Net::HTTP::Post.new(uri.request_uri,
'Content-Type' => 'application/json')
request.body = newAcctJson
resp = http.request(request)
To combine the code from the question and #DVG's excellent answer:
require 'net/http'
#require 'json' - not needed for the example
require 'uri'
#test data
newAcctJson ='{
"type": "Credit Card",
"nickname": "MoreTesting",
"rewards": 2,
"balance": 50
}'
#creates a new account
def createAcct(custID, json)
url = "http://api.reimaginebanking.com:80/customers/#{custID}/accounts?key=#{APIkey}"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(
uri.request_uri,
'Content-Type' => 'application/json'
)
request.body = newAcctJson
response = http.request(request)
puts(response.body)
end

Ruby https POST with headers

How can I make a Https post with a header in Ruby with a json?
I have tried:
uri = URI.parse("https://...")
https = Net::HTTP.new(uri.host,uri.port)
req = Net::HTTP::Post.new(uri.path)
req['foo'] = bar
res = https.request(req)
puts res.body
The problem it was a json. This solve my problem. Anyway, my question was not clear, so the bounty goes to Juri
require 'uri'
require 'net/http'
require 'net/https'
require 'json'
#toSend = {
"date" => "2012-07-02",
"aaaa" => "bbbbb",
"cccc" => "dddd"
}.to_json
uri = URI.parse("https:/...")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'})
req['foo'] = 'bar'
req.body = "[ #{#toSend} ]"
res = https.request(req)
puts "Response #{res.code} #{res.message}: #{res.body}"
Try:
require 'net/http'
require 'net/https'
uri = URI.parse("https://...")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
req = Net::HTTP::Post.new(uri.path)
req['foo'] = bar
res = https.request(req)
puts res.body
Here's a cleaner way to use Net::HTTP. If you just want to get the response and throw other objects away this is quite useful.
require 'net/http'
require 'json'
uri = URI("https://example.com/path")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Post.new(uri)
req['Content-Type'] = 'application/json'
# The body needs to be a JSON string, use whatever you know to parse Hash to JSON
req.body = {a: 1}.to_json
http.request(req)
end
# The "res" is what you need, get content from "res.body". It's a JSON string too.
A secure-by-default example:
require 'net/http'
require 'net/https'
req = Net::HTTP::Post.new("/some/page.json", {'Content-Type' =>'application/json'})
req.body = your_post_body_json_or_whatever
http = Net::HTTP.new('www.example.com', 443)
http.use_ssl = true
http.ssl_version = :TLSv1 # ruby >= 2.0 supports :TLSv1_1 and :TLSv1_2.
# SSLv3 is broken at time of writing (POODLE), and it's old anyway.
http.verify_mode = OpenSSL::SSL::VERIFY_PEER # please don't use verify_none.
# if you want to verify a server is from a certain signing authority,
# (self-signed certs, for example), do this:
http.ca_file = 'my-signing-authority.crt'
response = http.start {|http| http.request(req) }
Its working, you can pass data and header like this:
header = {header part}
data = {"a"=> "123"}
uri = URI.parse("https://anyurl.com")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
req = Net::HTTP::Post.new(uri.path, header)
req.body = data.to_json
res = https.request(req)
puts "Response #{res.code} #{res.message}: #{res.body}"

Ruby send JSON request

How do I send a JSON request in ruby? I have a JSON object but I dont think I can just do .send. Do I have to have javascript send the form?
Or can I use the net/http class in ruby?
With header - content type = json and body the json object?
uri = URI('https://myapp.com/api/v1/resource')
body = { param1: 'some value', param2: 'some other value' }
headers = { 'Content-Type': 'application/json' }
response = Net::HTTP.post(uri, body.to_json, headers)
require 'net/http'
require 'json'
def create_agent
uri = URI('http://api.nsa.gov:1337/agent')
http = Net::HTTP.new(uri.host, uri.port)
req = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
req.body = {name: 'John Doe', role: 'agent'}.to_json
res = http.request(req)
puts "response #{res.body}"
rescue => e
puts "failed #{e}"
end
HTTParty makes this a bit easier I think (and works with nested json etc, which didn't seem to work in other examples I've seen.
require 'httparty'
HTTParty.post("http://localhost:3000/api/v1/users", body: {user: {email: 'user1#example.com', password: 'secret'}}).body
This works on ruby 2.4 HTTPS Post with JSON object and the response body written out.
require 'net/http' #net/https does not have to be required anymore
require 'json'
require 'uri'
uri = URI('https://your.secure-url.com')
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
request.body = {parameter: 'value'}.to_json
response = http.request request # Net::HTTPResponse object
puts "response #{response.body}"
end
real life example, notify Airbrake API about new deployment via NetHttps
require 'uri'
require 'net/https'
require 'json'
class MakeHttpsRequest
def call(url, hash_json)
uri = URI.parse(url)
req = Net::HTTP::Post.new(uri.to_s)
req.body = hash_json.to_json
req['Content-Type'] = 'application/json'
# ... set more request headers
response = https(uri).request(req)
response.body
end
private
def https(uri)
Net::HTTP.new(uri.host, uri.port).tap do |http|
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
end
end
project_id = 'yyyyyy'
project_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
url = "https://airbrake.io/api/v4/projects/#{project_id}/deploys?key=#{project_key}"
body_hash = {
"environment":"production",
"username":"tomas",
"repository":"https://github.com/equivalent/scrapbook2",
"revision":"live-20160905_0001",
"version":"v2.0"
}
puts MakeHttpsRequest.new.call(url, body_hash)
Notes:
in case you doing authentication via Authorisation header set header req['Authorization'] = "Token xxxxxxxxxxxx" or http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Token.html
A simple json POST request example for those that need it even simpler than what Tom is linking to:
require 'net/http'
uri = URI.parse("http://www.example.com/search.json")
response = Net::HTTP.post_form(uri, {"search" => "Berlin"})
I like this light weight http request client called `unirest'
gem install unirest
usage:
response = Unirest.post "http://httpbin.org/post",
headers:{ "Accept" => "application/json" },
parameters:{ :age => 23, :foo => "bar" }
response.code # Status code
response.headers # Response headers
response.body # Parsed body
response.raw_body # Unparsed body
It's 2020 - nobody should be using Net::HTTP any more and all answers seem to be saying so, use a more high level gem such as Faraday - Github
That said, what I like to do is a wrapper around the HTTP api call,something that's called like
rv = Transporter::FaradayHttp[url, options]
because this allows me to fake HTTP calls without additional dependencies, ie:
if InfoSig.env?(:test) && !(url.to_s =~ /localhost/)
response_body = FakerForTests[url: url, options: options]
else
conn = Faraday::Connection.new url, connection_options
Where the faker looks something like this
I know there are HTTP mocking/stubbing frameworks, but at least when I researched last time they didn't allow me to validate requests efficiently and they were just for HTTP, not for example for raw TCP exchanges, this system allows me to have a unified framework for all API communication.
Assuming you just want to quick&dirty convert a hash to json, send the json to a remote host to test an API and parse response to ruby this is probably fastest way without involving additional gems:
JSON.load `curl -H 'Content-Type:application/json' -H 'Accept:application/json' -X POST localhost:3000/simple_api -d '#{message.to_json}'`
Hopefully this goes without saying, but don't use this in production.
The net/http api can be tough to use.
require "net/http"
uri = URI.parse(uri)
Net::HTTP.new(uri.host, uri.port).start do |client|
request = Net::HTTP::Post.new(uri.path)
request.body = "{}"
request["Content-Type"] = "application/json"
client.request(request)
end
data = {a: {b: [1, 2]}}.to_json
uri = URI 'https://myapp.com/api/v1/resource'
https = Net::HTTP.new uri.host, uri.port
https.use_ssl = true
https.post2 uri.path, data, 'Content-Type' => 'application/json'
Using my favourite http request library in ruby:
resp = HTTP.timeout(connect: 15, read: 30).accept(:json).get('https://units.d8u.us/money/1/USD/GBP/', json: {iAmOne: 'Hash'}).parse
resp.class
=> Hash

Resources