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

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

Related

Unauthorized readbody=true when posting message to hangout chat room with webhook

Trying to post a message to a hangout chat room. I've generated a webhook for the room and used it as uri in the following code. The rest are basic net/http stuff.
require 'net/http'
require 'uri'
message = 'hello'
# prep and send the http request
uri = URI.parse("https://chat.googleapis.com/v1/spaces/AAAAcroWtl4/messages?key=abc&token=xyz")
request = Net::HTTP::Post.new(uri)
request.content_type = "application/json"
request.body = '"content":[{"type":"text","text":"'+message+'"}]'
req_options = { use_ssl: uri.scheme == "https" }
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
puts response.inspect
The response contains the following text.
#<Net::HTTPUnauthorized 401 Unauthorized readbody=true>
Is there something wrong with the request body?
Edit: key and token changed in the question.
This same code worked with stride rooms, the only difference is that it had
request["Authorization"] = "Bearer #{access_token}"
Since the token is already in the uri I figured this would not be needed.
This worked for me
require 'net/http'
require 'uri'
require 'json'
message = 'hello'
# prep and send the http request
uri = URI.parse("//webhook")
request = Net::HTTP::Post.new(uri)
request['Content-Type'] = "application/json"
request.body = { text: message }.to_json
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(request)
end
puts response.inspect

How to use httparty instead of net/http or curl

I have a curl request which works
curl -X GET -k 'https://APIADDRESSHERE' -u 'USERNAME:PASSWORD' -H 'Content-Type: application/json'
I can also get this working in ruby:
require 'net/http'
require 'uri'
require 'openssl'
uri = URI.parse("https://APIADDRESSHERE")
request = Net::HTTP::Get.new(uri)
request.basic_auth("USERNAME", "PASSWORD")
request.content_type = "application/json"
req_options = {
use_ssl: uri.scheme == "https",
verify_mode: OpenSSL::SSL::VERIFY_NONE,
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
However when I try this with httparty I keep getting a 401 error saying:
An Authentication object was not found in the SecurityContext This request requires HTTP authentication.
I have tried a lot of variations of the following but I'm stuck.
response = HTTParty.get('https://APIURLHERE', {"headers": { "Authorization:" => "BASE64ENCODEDUSERNAMEANDPASSWORD", "Content-Type" => "application/json" }})
Have you tried the following?
response = HTTParty.get('https://APIURLHERE', headers: { "Authorization:" => "BASE64ENCODEDUSERNAMEANDPASSWORD", "Content-Type" => "application/json" })
Ok I made a simple mistake somewhere along the way because the original solution from the similar post worked... Dont know how it didnt work 50 times last time I was working on this but it was clearly an error on my part somewhere.

How to convert curl to Ruby Net::HTTP with -X GET -G options?

I was using https://jhawthorn.github.io/curl-to-ruby/ to convert curl commands to Net::HTTP code. However the following cannot be converted using the jhawthorn resource:
curl -H "Content-type: application/json" -H "Authorization: Token token=$PAGERDUTY_ACCESS_KEY" -X GET -G --data-urlencode "since=2017-01-16" --data-urlencode "until=2017-01-17" "https://company.pagerduty.com/api/v1/schedules"
I have described my exact problem in this github issue: https://github.com/jhawthorn/curl-to-ruby/issues/8
This is my current function that uses the Net::HTTP gem:
#!/usr/bin/env ruby
require 'json'
require 'net/http'
require 'uri'
def get_pagerduty_hash(ending='')
uri = URI.parse("https://company.pagerduty.com/api/v1/schedules#{ending}")
request = Net::HTTP::Get.new(uri)
request.content_type = "application/json"
request["Authorization"] = "Token token=#{ENV['PAGERDUTY_ACCESS_KEY']}"
req_options = {
use_ssl: uri.scheme == "https",
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
return JSON.parse(response.body).to_hash
end
How can I change this to correctly use the date part of the original curl command:
-X GET -G --data-urlencode "since=2017-01-16" --data-urlencode "until=2017-01-17"
You have to use the URI.encode_www_form function:
#!/usr/bin/env ruby
require 'json'
require 'net/http'
require 'uri'
def get_pagerduty_hash(ending='')
uri = URI.parse("https://company.pagerduty.com/api/v1/schedules#{ending}")
params = { :since => '2017-01-16', :until => '2017-01-17' }
uri.query = URI.encode_www_form(params)
request = Net::HTTP::Get.new(uri)
request.content_type = "application/json"
request["Authorization"] = "Token token=#{ENV['PAGERDUTY_ACCESS_KEY']}"
req_options = {
use_ssl: uri.scheme == "https",
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
return JSON.parse(response.body).to_hash
end
urlencode means that the data is encoded in the URL
url = URI.parse('http://example.com')
url.query = "since=2017-01-16&until=2017-01-17"
puts url
# => http://example.com?since=2017-01-16&until=2017-01-17

Curl get request in Ruby

Would be very grateful if someone could help me convert this curl request into ruby?I have Been trying for a while and can't get the syntax correct.
curl -v -H "Content-Type: application/json" -H "X-Knack-Application-Id:000000" -H "X-Knack-REST-API-Key:000000" https://api.knackhq.com/v1/objects/object_6/records
tried:
uri = URI('https://api.knackhq.com/v1/objects/object_1/records')
Net::HTTP.start(uri.host, uri.port,
:use_ssl => uri.scheme == 'https') do |http|
request = Net::HTTP::Post.new uri
# API details of Knack
request["X-Knack-Application-Id"] = '56e72cd003219158'
request["X-Knack-REST-API-Key"] = 'd9c343d2-2a4b-291e0712a63a'
end
There are at least two issues.
First, in the cURL command you don't specify a method, hence by default it is a GET, but you use POST in Ruby.
Secondly, you are missing the part where you execute the HTTP request
http.request(request)
Here's the code:
uri = URI('https://api.knackhq.com/v1/objects/object_1/records')
req = Net::HTTP::Get.new(uri)
req["X-Knack-Application-Id"] = '56e72cd003219158'
req["X-Knack-REST-API-Key"] = 'd9c343d2-2a4b-291e0712a63a'
res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
http.request(req)
end
More examples are available in the Net::HTTP documentation.
Did you try some gems? I often use RestClient for api reqests, here is my example:
RestClient.post("https://api.knackhq.com/v1/objects/object_6/records", {}, {"X-Knack-REST-API-Key" => "000000", "X-Knack-Application-Id"=>"000000"})
More information about: https://github.com/rest-client/rest-client

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

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)

Resources