如何将带有用户名和密码的 curl 请求转换为 https 站点的 ruby NET::HTTP?
How to turn curl request with user and password into ruby NET::HTTP for https site?
我有一个 ruby 脚本,用于从网页获取信息并更新页面。我从网页中获取了一些 json 信息:
`curl -s -u #{username}:#{password} #{HTTPS_PAGE_URL}`
然后我更新页面:
`curl -s -u #{username}:#{password} -X PUT -H 'Content-Type: application/json' -d'#{new_page_json_info}' #{HTTPS_PAGE_URL}`
我想用 Net::HTTP
代替。我该怎么做?
供参考的是我最初用来创建 curl 命令的 confluence 文档:https://developer.atlassian.com/confdev/confluence-server-rest-api/confluence-rest-api-examples
可以尝试做类似的事情:
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")
谢谢http://jhawthorn.github.io/curl-to-ruby
那解决了它。您所要做的就是向该网站提供您的 curl 命令,它会将其转换为 ruby 脚本。
第一个 curl(从页面获取 json 信息并将其发送到标准输出):
#!/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
第二个(这会更新页面上的 json 信息):
#!/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
我有一个 ruby 脚本,用于从网页获取信息并更新页面。我从网页中获取了一些 json 信息:
`curl -s -u #{username}:#{password} #{HTTPS_PAGE_URL}`
然后我更新页面:
`curl -s -u #{username}:#{password} -X PUT -H 'Content-Type: application/json' -d'#{new_page_json_info}' #{HTTPS_PAGE_URL}`
我想用 Net::HTTP
代替。我该怎么做?
供参考的是我最初用来创建 curl 命令的 confluence 文档:https://developer.atlassian.com/confdev/confluence-server-rest-api/confluence-rest-api-examples
可以尝试做类似的事情:
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")
谢谢http://jhawthorn.github.io/curl-to-ruby 那解决了它。您所要做的就是向该网站提供您的 curl 命令,它会将其转换为 ruby 脚本。
第一个 curl(从页面获取 json 信息并将其发送到标准输出):
#!/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
第二个(这会更新页面上的 json 信息):
#!/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