1

How can I transform this curl command to a Ruby HTTP Request?

curl -k -v -X POST -H "Content-Type: application/json" --data-binary @/home/venafi/venafi-auth.json https://10.150.85.11/authorize/`

I don't know Ruby very well and I tried

require 'net/http'
require 'uri'
require 'openssl'
require 'json'

uri = URI.parse("https://10.150.85.1/authorize/")
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.path, initheader = {'Content-Type' =>'application/json'})
request.set_form_data({'Username' => 'user', 'Password' => 'Passw0rd'})
#request['foo'] = 'bar'
puts request
response =  http.request(request)
puts "Response\n #{response.code} \n#{response.message}: \n#{response.body.to_json}"
puts response
puts request

httpg = Net::HTTP.new(uri.host, uri.port)
httpg.use_ssl = true
httpg.verify_mode = OpenSSL::SSL::VERIFY_NONE
reqg = Net::HTTP::Get.new("uri.request_uri")

puts "Response\n #{reqg.body.to_json}"

But the only thing that I get is

Net::HTTP::Post:0x00000001baab98
Response
202
Accepted:
""
Net::HTTPAccepted:0x00000001bcac68
Net::HTTP::Post:0x00000001baab98
Response
null

While when I use curl I get an apikey.

4
  • Hello, welcome to SO. This is the start of a a good question, but in order for someone to answer it we need more information. Can you please edit the question to explain in what way the code doesn't work? SO focuses on fixing specific problems, so we need a specific problem to solve. Commented Oct 8, 2015 at 19:24
  • 2
    @WayneConrad I edited my question :) Commented Oct 9, 2015 at 15:07
  • Good that you've improved your question :). Please mind that I removed the sentence "Thank you" from your question. Since this is a Q&A based site, it is prefered to write clean questions, without sentences like "Thank you", "Hello fellow users" etc. Just a note ;). Welcome to SO. Commented Oct 9, 2015 at 17:46
  • Possible dupe of stackoverflow.com/questions/2024805/ruby-send-json-request Commented Oct 9, 2015 at 17:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.