Curl and Net Http Ruby

I can easily do it on the command line using curl:

curl --digest -u "user:password" [url] 

But I'm trying to use Ruby Net :: HTTP. Is there a way that I can provide my username and password when trying to use Net :: HTTP for the GET API?

I tried the basic_auth method, but it does not work.

here is my code snippet

basic auth

 uri = URI("https://www.commcarehq.org/a/chanderi/api/v0.4/form/?xmlns=http://openrosa.org/formdesigner/C3B60561-F631-4D4C-8611-10E975EF8B44") res = Net::HTTP.start(uri.hostname, uri.port) {|http| req = Net::HTTP::Get.new(uri) req.basic_auth 'myusername', 'mypassword' http.request(req) } puts res.body 

for network :: http :: digestauth

 digest_auth = Net::HTTP::DigestAuth.new uri = URI.parse 'http://localhost:8000/' uri.user = CGI.escape(' username@mail.com ') uri.password = 'password' h = Net::HTTP.new uri.host, uri.port req = Net::HTTP::Get.new uri.request_uri res = h.request req 

Net :: HTTPBadResponse: invalid status bar: ""

I follow http://docs.seattlerb.org/net-http-digest_auth/Net/HTTP/DigestAuth.html

+4
source share
5 answers

The basic_auth method is the right way to provide a username and password, but only if the server expects this form of authentication. If, as your curl snippet suggests, it expects digest authentication, then basic auth really doesn't work.

Net :: HTTP does not support auth assembly, but net-http-digest_auth adds support for it. The gemstone includes some example use .

+1
source

This is what I use:

 #The URL uri = URI.parse("http://lab") http = Net::HTTP.new(uri.host, uri.port) #Request URL request = Net::HTTP::Get.new(uri.request_uri) #Basic authentication request.basic_auth("userid", "secret") response = http.request(request) 
+1
source

Here is an example of basic auth in my Ruby Net :: HTTP cheat sheet: https://github.com/augustl/net-http-cheat-sheet/blob/master/basic_auth.rb

Code posting here, for completeness:

 require "net/http" require "uri" uri = URI.parse("http://google.com/") http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Get.new(uri.request_uri) request.basic_auth("username", "password") response = http.request(request) 

This works for me. Perhaps you are talking with a non-standard server?

+1
source

Here it is: Net :: HTTP is documented.

Creates a new Net :: HTTP object without opening a TCP connection or HTTP session.

The address must be a DNS host name or IP address, port is the port on which the server is running. If no port is specified, the default port is used for HTTP or HTTPS.

If none of the p_ arguments is specified, the host and port of the proxy are taken from the http_proxy environment variable (or its uppercase equivalent), if present. If the proxy requires authentication, you must provide it manually. For more information about proxy server detection in the environment, see URI :: Generi # find_proxy. To disable proxy checking, set p_addr to nil.

If you connect to a custom proxy, p_addr is the DNS name or IP address of the proxy host, p_port is the port for accessing the proxy server, and p_user and p_pass are the username and password, if authorization is required, use a proxy.

0
source

Comes with DigestAuth , you should do just that at http://docs.seattlerb.org/net-http-digest_auth/Net/HTTP/DigestAuth.html with the request twice since the second request (200) uses the first request error header (401 ) Check the auth variable:

 require 'uri' require 'net/http' require 'net/http/digest_auth' digest_auth = Net::HTTP::DigestAuth.new uri = URI.parse 'http://localhost:8000/' uri.user = 'username' uri.password = 'password' h = Net::HTTP.new uri.host, uri.port req = Net::HTTP::Get.new uri.request_uri res = h.request req # FIRST REQUEST: res is a 401 response with a WWW-Authenticate header auth = digest_auth.auth_header uri, res['www-authenticate'], 'GET' # create a new request with the Authorization header req = Net::HTTP::Get.new uri.request_uri req.add_field 'Authorization', auth # SECOND REQUEST: re-issue request with Authorization res = h.request req 
0
source

Source: https://habr.com/ru/post/1493931/


All Articles