HTTP.post_form in Ruby with custom headers

前端 未结 4 969
刺人心
刺人心 2021-01-14 20:07

Im trying to use Nets/HTTP to use POST and put in a custom user agent. I\'ve typically used open-uri but it cant do POST can it?

4条回答
  •  既然无缘
    2021-01-14 20:29

    require "net/http"
    
    uri = URI.parse('https://your_url.com')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.ca_path='/etc/pki/tls/certs/'
    http.ca_file='/etc/pki/tls/certs/YOUR_CERT_CHAIN_FILE'
    http.cert = OpenSSL::X509::Certificate.new(File.read("YOUR_CERT)_FILE"))
    http.key = OpenSSL::PKey::RSA.new(File.read("YOUR_KEY_FILE"))
    
    #SSLv3 is cracked, and often not allowed
    http.ssl_version = :TLSv1_2
    
    #### This is IMPORTANT
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    
    #Crete the POST request
    request = Net::HTTP::Post.new(uri.request_uri)
    request.add_field 'X_REMOTE_USER', 'soap_remote_user'
    request.add_field 'Accept', '*'
    request.add_field 'SOAPAction', 'soap_action'
    request.body = request_payload
    
    #Get Response
    response = http.request(request)
    
    #Review Response
    puts response.body
    

提交回复
热议问题