2012-06-29 34 views
25

Wie kann ich einen HTTP-Beitrag mit einem Header in Ruby mit einem JSON machen?Ruby https POST mit Kopfzeilen

Ich habe versucht:

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

Was ist der Fehler? –

Antwort

46

Das Problem es sich um eine json war. Das löst mein Problem. Wie auch immer, war meine Frage nicht klar, so dass die Prämie geht an 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

Der Code funktionierte irgendwie nicht für mich. Anstelle von @toSend = {}. To_json musste ich req.set_form_data (@toSend) eingeben, um meine Daten korrekt zu senden. Hoffentlich hilft das jemandem, der stecken geblieben ist. – Kirk

+0

Ich brauchte HTTPS nicht zu verwenden, aber fand eine funktionierende Lösung mit benutzerdefinierten Headern hier: http://Stackoverflow.com/a/36928680/396429 –

+1

sollte es sein 'initheader: {'Content-Type' => 'application/json '} ' –

31

Versuchen:

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

Wie lege ich den Körper und die Header fest? –

+0

'req.body =" Der Körper "' –

9

Ein sicheres-by-default Beispiel:

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

Wie lege ich den Körper und die Überschriften? –

+1

{'Content-Type' => 'application/json'} ist der Hash, der zu den Headern wird. "your_post_body_json_or_whatever" ist dein Körper. –

6

Hier ist ein sauberer Weg Net verwenden :: HTTP. Wenn Sie nur die Antwort erhalten und andere Objekte wegwerfen möchten, ist dies sehr nützlich.

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

Seine Arbeits können Sie Daten übergeben und wie diese Header:

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}"