2012-06-29 37 views
25

Làm cách nào để tạo một bài đăng Https với tiêu đề trong Ruby bằng json?Ruby https POST với tiêu đề

Tôi đã thử:

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 
+0

lỗi là gì? –

Trả lời

46

Vấn đề đó là một json. Điều này giải quyết vấn đề của tôi. Dù sao, câu hỏi của tôi là không rõ ràng, do đó tiền thưởng đi vào 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}" 
+1

Mã nào đó không hoạt động đối với tôi. Thay vì @toSend = {}. To_json, tôi phải làm req.set_form_data (@toSend) để gửi dữ liệu của mình đúng cách. Hy vọng rằng điều này sẽ giúp đỡ người khác bị mắc kẹt. – Kirk

+0

Tôi không cần sử dụng HTTPS nhưng đã tìm thấy giải pháp làm việc với tiêu đề tùy chỉnh tại đây: http://stackoverflow.com/a/36928680/396429 –

+1

nó phải là 'initheader: {' Content-Type '=>' application/json '} ' –

31

Hãy thử:

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 
+2

làm cách nào để đặt nội dung và tiêu đề? –

+0

'req.body =" Nội dung "' –

9

Một ví dụ an toàn theo mặc định:

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) } 
+0

làm cách nào để đặt nội dung và tiêu đề? –

+1

{'Content-Type' => 'application/json'} là băm trở thành tiêu đề. "your_post_body_json_or_whatever" là cơ thể của bạn. –

6

Đây là một cách sạch để sử dụng Net :: HTTP. Nếu bạn chỉ muốn nhận được phản hồi và ném các đối tượng khác đi thì điều này khá hữu ích.

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. 
2

làm việc của nó, bạn có thể truyền dữ liệu và tiêu đề như thế này:

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}" 
Các vấn đề liên quan