我创建了Web应用程序,我在向https服务器创建发布请求时遇到了一个问题。 我的get请求代码在这里:
uri = URI('https://test.com/all')
Net::HTTP.start(uri.host, uri.port,
:use_ssl => uri.scheme == 'https',
:verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
request = Net::HTTP::Get.new uri.request_uri
request.basic_auth 'dt', 'dt'
response = http.request request # Net::HTTPResponse object
return response.body
end
如何创建POST请求? 我不知道。
答案 0 :(得分:2)
我从使用帖子的谷歌授权方法中拿了一块,它可以更简单但是你可以看到如何使用params,ssl等
require 'net/http'
require 'net/https'
require 'uri'
@title = "Google Authetication"
token = params[:code]
client_secret = "***********************"
uri = URI('https://accounts.google.com/o/oauth2/token')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data('code' => token, 'client_id' => @client_id, 'client_secret' => client_secret, 'redirect_uri' => googleauth_url, 'grant_type' => 'authorization_code')
request.content_type = 'application/x-www-form-urlencoded'
response = http.request(request)
response.code
答案 1 :(得分:0)
示例:
uri = URI('http://www.example.com/search.cgi')
res = Net::HTTP.post_form(uri, 'q' => 'ruby', 'max' => '50')
puts res.body
答案 2 :(得分:0)
我设法做到了,我的工作代码:
uri = URI('https://test.com/olololo')
Net::HTTP.start(uri.host, uri.port,
:use_ssl => uri.scheme == 'https',
:verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
request = Net::HTTP::Post.new uri.request_uri
request.basic_auth 'dt', 'dt'
request.body = res_Data.to_json
response = http.request request
return response.body
end