HTTP Post Request失败 - Ruby Curl

时间:2016-05-16 14:12:34

标签: ruby http post curl http-post

我是Ruby的新手,因此,对语法不太熟悉 -

我想要一个用于以下curl请求的Ruby代码 -

curl -X POST http://<auth_server_endpoint>/oauth/token -d "grant_type
=client_credentials" -H"Authorization: Basic encode64Of(clientid:secret)"

关注链接后 - How to invoke HTTP POST method over SSL in ruby?

我已经写了一个功能来获得回复 -

def getToken(client_id, secret)

encodedId = Base64.encode64(client_id+":"+secret)

uri = URI.parse("auth_server_endpoint")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new("/oauth/token")
request.add_field('Content-Type', 'application/json')
request.add_field('Authorization', "Basic #{encodedId}")

// FAILS BELOW

response = http.request(request, {'grant_type' => 'client_credentials'})
parsed_json = JSON.parse(response)

self.access_token = parsed_json.fetch("access_token")
self.expires_in = parsed_json.fetch("expires_in")

end

我无法弄清楚错误。请建议我需要做出的改变。

错误日志 -

OpenSSL::SSL::SSLError - SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: unknown protocol:
    /netdir/http.rb:920:in `connect'
    /netdir/http.rb:920:in `block in connect'
    /rubydir/2.1.0/timeout.rb:76:in `timeout'
    /netdir/http.rb:920:in `connect'
    /netdir/http.rb:863:in `do_start'
    /netdir/http.rb:852:in `start'
    /netdir/http.rb:1369:in `request'
    /rubydir/gems/2.1.0/gems/rest-client-1.6.7/lib/restclient/net_http_ext.rb:51:in `request'
    /appdir/lib/sc_customer/auth_token.rb:30:in `getToken'
    /appdir/controllers/payment_controller.rb:276:in `block (2 levels) in <module:API>'
    /rubydir/gems/2.1.0/gems/rack-metrics-0.0.27/lib/probes/sinatra_probe.rb:24:in `call'
    /rubydir/gems/2.1.0/gems/rack-metrics-0.0.27/lib/probes/sinatra_probe.rb:24:in `block in bind'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:569:in `call'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:569:in `block in route'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `[]'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `block (3 levels) in process_destination_path'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:851:in `route_eval'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `block (2 levels) in process_destination_path'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `catch'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `block in process_destination_path'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:25:in `instance_eval'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:25:in `process_destination_path'
    (eval):16:in `block (2 levels) in inject_root_methods'
    (eval):6:in `catch'
    (eval):6:in `block in inject_root_methods'
    /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router/node/root.rb:554:in `[]'
    /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router/node/root.rb:13:in `[]'
    /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router.rb:119:in `block in call'
    /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router.rb:119:in `catch'
    /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router.rb:119:in `call'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:910:in `route!'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:900:in `dispatch!'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `block in call!'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `block in invoke'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `catch'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `invoke'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `call!'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:755:in `call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/logger.rb:15:in `call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/session/abstract/id.rb:195:in `context'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/session/abstract/id.rb:190:in `call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/head.rb:9:in `call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/methodoverride.rb:24:in `call'
    /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/sc_core/logger.rb:58:in `call'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `block in call'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:1471:in `synchronize'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `call'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/router.rb:83:in `block in call'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/router.rb:76:in `each'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/router.rb:76:in `call'
    /rubydir/gems/2.1.0/gems/sc-mq-0.0.86/lib/sc_mq/rack/filter.rb:24:in `call'
    /appdir/lib/rack/bu_aware_request_handler.rb:32:in `block in call'
    /rubydir/gems/2.1.0/gems/activerecord_use_slave-0.0.5/lib/activerecord_use_slave.rb:44:in `using_connection'
    /appdir/lib/rack/bu_aware_request_handler.rb:31:in `call'
    /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/rack/connection_release.rb:11:in `call'
    /rubydir/gems/2.1.0/gems/rack-metrics-0.0.27/lib/rack-metrics/rack.rb:11:in `call'
    /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/rack/serial_initializer.rb:49:in `call'
    /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/rack/log_exceptions.rb:11:in `call'
    /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/rack/transaction_tracer.rb:23:in `call'
    /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/sc_core/rack/filter.rb:14:in `call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/lint.rb:48:in `_call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/lint.rb:36:in `call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/showexceptions.rb:24:in `call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/commonlogger.rb:20:in `call'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:136:in `call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/chunked.rb:41:in `call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/content_length.rb:14:in `call'
    /rubydir/gems/2.1.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:552:in `process_client'
    /rubydir/gems/2.1.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:632:in `worker_loop'
    /rubydir/gems/2.1.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:500:in `spawn_missing_workers'
    /rubydir/gems/2.1.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:142:in `start'
    /rubydir/gems/2.1.0/gems/unicorn-4.6.3/bin/unicorn:126:in `<top (required)>'
    /b2cdir/bin/unicorn:16:in `load'
    /b2cdir/bin/unicorn:16:in `<main>'
  ERROR -17/May/2016 10:56:42 [payment-b2c TXN-af2c9519-6e89-4fa2-8913-ee164f53523d REQ-28485d83-47a3-47cc-814d-993dc544697a] - OpenSSL::SSL::SSLError - SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: unknown protocol:
 /netdir/http.rb:920:in `connect'
 /netdir/http.rb:920:in `block in connect'
 /rubydir/2.1.0/timeout.rb:76:in `timeout'
 /netdir/http.rb:920:in `connect'
 /netdir/http.rb:863:in `do_start'
 /netdir/http.rb:852:in `start'
 /netdir/http.rb:1369:in `request'
 /rubydir/gems/2.1.0/gems/rest-client-1.6.7/lib/restclient/net_http_ext.rb:51:in `request'
 /appdir/lib/sc_customer/auth_token.rb:30:in `getToken'
 /appdir/controllers/payment_controller.rb:276:in `block (2 levels) in <module:API>'
 /rubydir/gems/2.1.0/gems/rack-metrics-0.0.27/lib/probes/sinatra_probe.rb:24:in `call'
 /rubydir/gems/2.1.0/gems/rack-metrics-0.0.27/lib/probes/sinatra_probe.rb:24:in `block in bind'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:569:in `call'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:569:in `block in route'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `[]'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `block (3 levels) in process_destination_path'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:851:in `route_eval'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `block (2 levels) in process_destination_path'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `catch'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `block in process_destination_path'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:25:in `instance_eval'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:25:in `process_destination_path'
 (eval):16:in `block (2 levels) in inject_root_methods'
 (eval):6:in `catch'
 (eval):6:in `block in inject_root_methods'
 /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router/node/root.rb:554:in `[]'
 /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router/node/root.rb:13:in `[]'
 /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router.rb:119:in `block in call'
 /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router.rb:119:in `catch'
 /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router.rb:119:in `call'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:910:in `route!'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:900:in `dispatch!'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `block in call!'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `block in invoke'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `catch'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `invoke'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `call!'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:755:in `call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/logger.rb:15:in `call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/session/abstract/id.rb:195:in `context'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/session/abstract/id.rb:190:in `call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/head.rb:9:in `call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/methodoverride.rb:24:in `call'
 /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/sc_core/logger.rb:58:in `call'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `block in call'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:1471:in `synchronize'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `call'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/router.rb:83:in `block in call'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/router.rb:76:in `each'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/router.rb:76:in `call'
 /rubydir/gems/2.1.0/gems/sc-mq-0.0.86/lib/sc_mq/rack/filter.rb:24:in `call'
 /appdir/lib/rack/bu_aware_request_handler.rb:32:in `block in call'
 /rubydir/gems/2.1.0/gems/activerecord_use_slave-0.0.5/lib/activerecord_use_slave.rb:44:in `using_connection'
 /appdir/lib/rack/bu_aware_request_handler.rb:31:in `call'
 /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/rack/connection_release.rb:11:in `call'
 /rubydir/gems/2.1.0/gems/rack-metrics-0.0.27/lib/rack-metrics/rack.rb:11:in `call'
 /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/rack/serial_initializer.rb:49:in `call'
 /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/rack/log_exceptions.rb:11:in `call'
 /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/rack/transaction_tracer.rb:23:in `call'
 /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/sc_core/rack/filter.rb:14:in `call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/lint.rb:48:in `_call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/lint.rb:36:in `call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/showexceptions.rb:24:in `call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/commonlogger.rb:20:in `call'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:136:in `call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/chunked.rb:41:in `call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/content_length.rb:14:in `call'
 /rubydir/gems/2.1.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:552:in `process_client'
 /rubydir/gems/2.1.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:632:in `worker_loop'
 /rubydir/gems/2.1.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:500:in `spawn_missing_workers'
 /rubydir/gems/2.1.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:142:in `start'
 /rubydir/gems/2.1.0/gems/unicorn-4.6.3/bin/unicorn:126:in `<top (required)>'
 /b2cdir/bin/unicorn:16:in `load'
 /b2cdir/bin/unicorn:16:in `<main>'
  DEBUG -17/May/2016 10:56:42 [payment-b2c TXN-af2c9519-6e89-4fa2-8913-ee164f53523d REQ-28485d83-47a3-47cc-814d-993dc544697a] - POST [1.085s DB: 0.009s ( 0.8%) 7 queries] /payments - 500 Internal Server Error

0 个答案:

没有答案