Reputation: 775
I need to make a GET request in a public api. I know the login and password data are correct but the api returns authentication error. By postman the request is made successfully. The problem is in the same ruby code.
code:
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
I make a request on a POST route in the same way and it WORKS. I do not understand why with GET does not work.
With post method works:
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
In postman:
method: post basic auth: username admin password 123mudar
Upvotes: 1
Views: 503
Reputation: 44
In the case, you can't pass form data to GET method.Look at example bellow, I wrote a piece of code based on examples given in the Net::HTTP docs and tested it on my local - it works. Here's what I have:
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
Upvotes: 2