0

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

Alex Cohen
  • 5,596
  • 16
  • 54
  • 104

2 Answers2

0

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")
harshit
  • 7,925
  • 23
  • 70
  • 97
0

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
Alex Cohen
  • 5,596
  • 16
  • 54
  • 104