I am trying to understand the Railscast: SOAP with Savon and implement it for my scenario.
I have executed the following in rails console
realm = Base64.strict_encode64("user:password")
client = Savon.client(wsdl: "http://xxxxx?wsdl", soap_header: { 'Authorization:' => "Basic #{realm}"}, log: true, log_level: :debug, pretty_print_xml: true)
message = {"X1" => "XX", "X2" => 108539, "X3" => 2, "CONFIRM" => TRUE}
All fine so far. The following line is throwing an error
response = client.call(:update_job, message: message)
Please can you let me know how to resolve the following error or any useful documentation that I can look at to fix the error. (I have tried searching online and also looked at http://savonrb.com/version2/requests.html)
HTTPI POST request to sit (net_http)
SOAP response (status 500)
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd=
"http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-i
nstance">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>Unexpected character '>' (code 62) (expected a name start
character)
at [row,col {unknown-source}]: [1,434]</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
Savon::SOAPFault: (soap:Server) Unexpected character '>' (code 62) (expected a n
ame start character)
at [row,col {unknown-source}]: [1,434]
from c:/jruby-1.7.12/lib/ruby/gems/shared/gems/savon-2.6.0/lib/savon/res
ponse.rb:85:in `raise_soap_and_http_errors!'
from c:/jruby-1.7.12/lib/ruby/gems/shared/gems/savon-2.6.0/lib/savon/res
ponse.rb:14:in `initialize'
from c:/jruby-1.7.12/lib/ruby/gems/shared/gems/savon-2.6.0/lib/savon/ope
ration.rb:64:in `create_response'
from c:/jruby-1.7.12/lib/ruby/gems/shared/gems/savon-2.6.0/lib/savon/ope
ration.rb:55:in `call'
from c:/jruby-1.7.12/lib/ruby/gems/shared/gems/savon-2.6.0/lib/savon/cli
ent.rb:36:in `call'
from (irb):75:in `evaluate'
from org/jruby/RubyKernel.java:1101:in `eval'
from org/jruby/RubyKernel.java:1501:in `loop'
from org/jruby/RubyKernel.java:1264:in `catch'
from org/jruby/RubyKernel.java:1264:in `catch'
from c:/jruby-1.7.12/lib/ruby/gems/shared/gems/railties-3.2.13/lib/rails
/commands/console.rb:47:in `start'
from c:/jruby-1.7.12/lib/ruby/gems/shared/gems/railties-3.2.13/lib/rails
/commands/console.rb:8:in `start'
from c:/jruby-1.7.12/lib/ruby/gems/shared/gems/railties-3.2.13/lib/rails
/commands.rb:41:in `(root)'
from org/jruby/RubyKernel.java:1065:in `require'
from script/rails:6:in `(root)'
Thanks.
Your soap_header should not be postfixed with : (Authorization:).
client = Savon.client(
wsdl: "http://xxxxx?wsdl",
soap_header: { 'Authorization' => "Basic #{realm }"},
log: true,
log_level: :debug,
pretty_print_xml: true
)
http://savonrb.com/version2/globals.html
Related
I am trying to make HTTPS API request to Salesforce instance. I am trying to connect from localhost and windows machine.
SALESFORCE_URL = "https://my.salesforce.com"
TOKEN_URL = "services/oauth2/token"
request_params = {
"grant_type" => "password",
"client_id" => "3MVG9gOZsF7exF8Pn79zdsxuntzX_0TlVPRKL0R8iPxBQ8dHqh14yzg",
"client_secret" => "272142298328091392325",
"redirect_url" => "https://localhost:3000/RestTest/oauth/_callback",
"username" => "user#xyz.com",
"password" => "xyz#2018",
}
url = SALESFORCE_URL + "/" + TOKEN_URL
uri = URI.parse(url)
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.body = request_params.to_json
response = http.request(request)
Rails.logger.info response.inspect
I am getting below error.
OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: sslv3 alert handshake failure
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:800:in `connect'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:800:in `block in connect'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/timeout.rb:55:in `timeout'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/timeout.rb:100:in `timeout'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:800:in `connect'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:756:in `do_start'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:745:in `start'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/right_http_connection-1.3.0/lib/net_fix.rb:129:in `request'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/net_http_ext.rb:51:in `request'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/newrelic_rpm-3.7.1.188/lib/new_relic/agent/instrumentation/net.rb:27:in `block (2 levels) in request_with_newrelic_trace'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/newrelic_rpm-3.7.1.188/lib/new_relic/agent.rb:400:in `disable_all_tracing'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/newrelic_rpm-3.7.1.188/lib/new_relic/agent/instrumentation/net.rb:26:in `block in request_with_newrelic_trace'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/newrelic_rpm-3.7.1.188/lib/new_relic/agent/cross_app_tracing.rb:41:in `trace_http_request'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/newrelic_rpm-3.7.1.188/lib/new_relic/agent/instrumentation/net.rb:23:in `request_with_newrelic_trace'
C:/Users/user/Home/projects/portcullis/mainNew/lib/tasks/salesforce.rake:41:in `block (2 levels) in <top (required)>'
Why I am getting this error?
Salesforce does not support SSLv3 (POODLE Vulnerability).
Check the suported ciphers using online tools like Comodo's SSL Analyzer or Qualys SSL Labs and configure your HTTP client
connection.ssl_version = :TLSv1 or :TLSv1_2
I have this problem with an simple request with mechanize in my production code, in local works ok, but in production environment not, can you help me? thanks.
agent = Mechanize.new
agent.log = Logger.new $stderr
agent.agent.http.debug_output = $stderr
page = agent.get "THE_URL_EXAMPLE"
I get this error:
too many connection resets (due to end of file reached - EOFError) after 0 requests on 54261840, last used 1491919097.3684318 seconds ago
I am working with ruby 2.0.0 and mechanize 2.7.5
I try change the keep_alive to false, add an idle_timeout, this code: http://scottwb.com/blog/2013/11/09/defeating-the-infamous-mechanize-too-many-connection-resets-bug/ but nothing work.
2.0.0 :119 > page = agent.get uri
Net::HTTP::Get: MY_ACTION_EXAMPLE
request-header: accept-encoding => gzip,deflate,identity
request-header: accept => /
request-header: user-agent => Mechanize/2.7.5 Ruby/2.0.0p451 (http://github.com/sparklemotion/mechanize/)
request-header: connection => close
request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
request-header: accept-language => en-us,en;q=0.5
request-header: host => MY_HOST_EXAMPLE
request-header: x-requested-with => XMLHttpRequest
opening connection to MY_HOST_EXAMPLE:80...
opened
<- "GET MY_ACTION_EXAMPLE HTTP/1.1\r\nAccept-Encoding: gzip,deflate,identity\r\nAccept: /\r\nUser-Agent: Mechanize/2.7.5 Ruby/2.0.0p451 (http://github.com/sparklemotion/mechanize/)\r\nConnection: close\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nAccept-Language: en-us,en;q=0.5\r\nHost: MY_HOST_EXAMPLE\r\nX-Requested-With: XMLHttpRequest\r\n\r\n"
Conn close because of error end of file reached, and retry
opening connection to MY_HOST_EXAMPLE:80...
opened
<- "GET MY_ACTION_EXAMPLE HTTP/1.1\r\nAccept-Encoding: gzip,deflate,identity\r\nAccept: /\r\nUser-Agent: Mechanize/2.7.5 Ruby/2.0.0p451 (http://github.com/sparklemotion/mechanize/)\r\nConnection: close\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nAccept-Language: en-us,en;q=0.5\r\nHost: MY_HOST_EXAMPLE\r\nX-Requested-With: XMLHttpRequest\r\n\r\n"
Conn close because of error end of file reached
Conn close because of error end of file reached
Net::HTTP::Persistent::Error: too many connection resets (due to end of file reached - EOFError) after 0 requests on 67857800, last used 1491937635.2713494 seconds ago
from /usr/local/rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/protocol.rb:153:in `read_nonblock'
from /usr/local/rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/protocol.rb:153:in `rbuf_fill'
from /usr/local/rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/protocol.rb:134:in `readuntil'
from /usr/local/rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/protocol.rb:144:in `readline'
from /usr/local/rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http/response.rb:39:in `read_status_line'
from /usr/local/rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http/response.rb:28:in `read_new'
from /usr/local/rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http.rb:1406:in `block in transport_request'
from /usr/local/rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http.rb:1403:in `catch'
from /usr/local/rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http.rb:1403:in `transport_request'
from /usr/local/rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/net/http.rb:1376:in `request'
from /home/deploy/www/shared/bundle/ruby/2.0.0/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:999:in `request'
from /home/deploy/www/shared/bundle/ruby/2.0.0/gems/mechanize-2.7.5/lib/mechanize/http/agent.rb:274:in `fetch'
from /home/deploy/www/shared/bundle/ruby/2.0.0/gems/mechanize-2.7.5/lib/mechanize.rb:464:in `get'
from (irb):119
from /home/deploy/www/shared/bundle/ruby/2.0.0/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /home/deploy/www/shared/bundle/ruby/2.0.0/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /home/deploy/www/shared/bundle/ruby/2.0.0/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'2.0.0 :120 >
And it is only that url that is the strangest yet..
Please help me. Thanks.
For some reason when I run this in my Rails console it is not working.
RestClient.post 'http://localhost/WebService/AuthenticateLogin', :content_type => :json, {:params => {:RuntimeEnvironment => 1, 'Email' => 'someone#example.com', 'Password' => 'Pa$$worD'}}
The odd part is when I do the same with cURL it works perfectly fine:
curl -H "Content-Type: application/json" -d '{"RuntimeEnvironment":1,"Email":"someone#example.com","Password":"Pa$$worD"}' -X POST http://localhost/WebService/AuthenticateLogin
Here is my stacktrace:
SyntaxError: (irb):33: syntax error, unexpected '\n', expecting =>
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
And because I think it may be relevant, I used this statement as well:
RestClient.post 'http://localhost/WebService/AuthenticateLogin', :content_type => :json, :params => {:RuntimeEnvironment => 1, 'Email' => 'someone#example.com', 'Password' => 'Pa$$worD'}
And got this as my stacktrace:
RestClient.post "http://localhost/WebService/AuthenticateLogin", "content_type=json¶ms[RuntimeEnvironment]=1¶ms[Email]=someone%40example.com¶ms[Password]=Pa$$worD", "Accept"=>"*/*; q=0.5, application/
xml", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"114", "Content-Type"=>"application/x-www-form-urlencoded"
# => 400 BadRequest | text/html 2738 bytes
RestClient::BadRequest: 400 Bad Request
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rest-client-1.6.7/lib/restclient/abstract_response.rb:48:in `return!'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rest-client-1.6.7/lib/restclient/request.rb:230:in `process_result'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rest-client-1.6.7/lib/restclient/request.rb:178:in `block in transmit'
from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:852:in `start'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rest-client-1.6.7/lib/restclient/request.rb:172:in `transmit'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rest-client-1.6.7/lib/restclient.rb:72:in `post'
from (irb):34
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
I have scoured Stack and tried different ways of listing out the params, moving/removing the content_type, etc. Nothing is working for me.
You don't need to nest the params like that. Just do:
RestClient.post 'http://localhost/WebService/AuthenticateLogin', {"RuntimeEnvironment" => 1, 'Email' => 'someone#example.com', 'Password' => 'Pa$$worD'}, :content_type => :json
I noticed my content_type was not being updated, searched it up and StackOverflow came through for me.
How do I make Ruby's RestClient gem respect content_type on post?
I basically just changed my request to be:
RestClient.post 'http://localhost/WebService/AuthenticateLogin', '{"RuntimeEnvironment":1, "Email":"someone#example.com", "Password":"Pa$$worD"}', :content_type => "json"
I merely added quotes around my params like so: '{params}'
And instead of using => in the params, I changed to colons just like the actual request I used in cURL and everything worked like a charm.
Also note I quoted the "json" content_type.
Thanks #rlarcombe for your help.
I have to consume methods from an external API provided, and one method is a GET with a json body. I'm making the connection through rails using rest-client gem that makes that job.
I know that RestClient.post accepts json:
RestClient.post "http://example.com/resource", { 'x' => 1 }.to_json, :content_type => :json, :accept => :json
And also for my case works perfectly. But if i change the .post to .get, i get this error:
ArgumentError: wrong number of arguments (3 for 2)
from /Users/toptierlabs/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/rest-client-1.6.7/lib/restclient.rb:67:in `get'
from /Users/toptierlabs/Desktop/Proyectos/AppraisalLane/app/models/autoniq.rb:8:in `getCircleInventory'
from (irb):3
from /Users/toptierlabs/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /Users/toptierlabs/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /Users/toptierlabs/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Is there any way in Rails to do a GET with a json body? Or any possible solution to translate a "get with a json body" to something else? Is not necessary to use the rest-client gem, if any one have another solution is welcome too.
Thanks!
Solved:
RestClient.get "http://example.com/resource", params: {'json' => { 'x' => 1 }.to_json},:content_type => :json, :accept => :json
After using timeout:
status = Timeout::timeout(5) {
# Something that should be interrupted if it takes too much time...
}
I got this Timeout error:
/Users/galharth/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `initialize': execution expired (Timeout::Error)
from /Users/galharth/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `open'
from /Users/galharth/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `block in connect'
from /Users/galharth/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/timeout.rb:44:in `timeout'
from /Users/galharth/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/timeout.rb:87:in `timeout'
from /Users/galharth/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `connect'
from /Users/galharth/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:637:in `do_start'
from /Users/galharth/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:632:in `start'
from /Users/galharth/.rvm/gems/ruby-1.9.2-p136/gems/mechanize-1.0.0/lib/mechanize.rb:527:in `fetch_page'
from /Users/galharth/.rvm/gems/ruby-1.9.2-p136/gems/mechanize-1.0.0/lib/mechanize.rb:259:in `get'
What should I do?
Well, that's expected behaviour of Timeout. If the block takes too long, its execution gets terminated and an exception thrown.
You would probably like to catch the exception and handle it appropriately:
require 'timeout'
begin
status = Timeout::timeout(5) {
# Something that should be interrupted if it takes too much time...
}
rescue Timeout::Error
puts 'That took too long, exiting...'
end
uri = URI.parse("https://example.com")
# Full control
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
#http.read_timeout = 500
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({"locationcode" => "999", "sublocationcode" => "1"})
response = http.request(request)
render :json => response.body