2012-06-29 21 views
25

Jak mogę utworzyć post Https z nagłówkiem w Ruby z jsonem?Ruby https POST z nagłówkami

Próbowałem:

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

Jaki jest błąd? –

Odpowiedz

46

Problemem było to json. To rozwiązuje mój problem. W każdym razie, moje pytanie nie było jasne, więc nagroda idzie do 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

Kod jakoś nie zadziałał dla mnie. Zamiast @toSend = {}. To_json musiałem wykonać polecenie set_form_data (@toSend), aby poprawnie wysłać moje dane. Mam nadzieję, że pomoże to komuś, kto utknął. – Kirk

+0

Nie musiałem używać HTTPS, ale znalazłem działające rozwiązanie z niestandardowymi nagłówkami tutaj: http://stackoverflow.com/a/36928680/396429 –

+1

powinno to być 'initheader: {'Content-Type' => 'application/json '} ' –

31

Spróbuj:

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

Jak ustawić treść i nagłówki? –

+0

'req.body =" Ciało "' –

9

Bezpieczny-by-default przykład:

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

jak ustawić ciało i nagłówki? –

+1

{'Content-Type' => 'application/json'} to skrót, który staje się nagłówkiem. "your_post_body_json_or_whatever" to twoje ciało. –

6

Oto odkurzacz sposobem korzystania Net :: HTTP. Jeśli chcesz uzyskać odpowiedź i odrzucić inne obiekty, jest to całkiem przydatne.

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

Jego działanie można przekazać dane i nagłówek tak:

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}" 
Powiązane problemy