我需要在公共API中发出GET请求。我知道登录和密码数据是正确的,但api返回验证错误。通过邮递员,请求成功。问题出在同一个红宝石代码中。
代码:
nfe_key = '41170608187168000160550010000001561000000053'
params = {'grupo' => 'edoc','cnpj' => '08187168000160', 'ChaveNota' => nfe_key, 'Url' => '1'}
url = URI.parse('https://managersaashom.tecnospeed.com.br:7071/ManagerAPIWeb/nfe/imprime')
get = Net::HTTP::Get.new(url.path)
get.basic_auth 'admin', '123mudar'
get.set_form_data(params)
request = Net::HTTP.new(url.host, url.port)
request.read_timeout = 500 #seconds
request.use_ssl = true
request.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = request.start {|http| http.request(get) }
puts response.body
我以相同的方式在POST路由上发出请求并且工作正常。我不明白为什么GET不起作用。
使用post方法工作:
params = {'grupo' => 'edoc','cnpj' => '08187168000160', 'arquivo' => 'formato=XML
' + xml}
url = URI.parse('https://managersaashom.tecnospeed.com.br:7071/ManagerAPIWeb/nfe/envia')
post = Net::HTTP::Post.new(url.path)
post.basic_auth 'admin', '123mudar'
post.set_form_data(params)
request = Net::HTTP.new(url.host, url.port)
request.read_timeout = 500 #seconds
request.use_ssl = true
request.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = request.start {|http| http.request(post) }
puts response.body
邮递员:
方法:发帖 基本认证: 用户名admin 密码123mudar
答案 0 :(得分:2)
在这种情况下,你不能将表单数据传递给GET方法。看看下面的例子,我根据Net :: HTTP文档中给出的示例编写了一段代码,并在我的本地测试了它 - 它的工作原理。这就是我所拥有的:
nfe_key = '41170608187168000160550010000001561000000053'
params = {'grupo' => 'edoc','cnpj' => '08187168000160', 'ChaveNota' => nfe_key, 'Url' => '1'}
uri = URI.parse('https://managersaashom.tecnospeed.com.br:7071/ManagerAPIWeb/nfe/imprime')
# Add params to URI
uri.query = URI.encode_www_form( params )
Net::HTTP.start(uri.host, uri.port,
:use_ssl => uri.scheme == 'https',
:verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
request = Net::HTTP::Get.new uri.request_uri
request.basic_auth 'admin', '123mudar'
response = http.request request # Net::HTTPResponse object
puts response
puts response.body
end