toland / patron

Ruby HTTP client based on libcurl
http://toland.github.com/patron/
MIT License
541 stars 74 forks source link

Basic Auth #185

Closed SaimonL closed 3 years ago

SaimonL commented 3 years ago

This maybe a silly question. How do I deal with http basic auth?

Do I do something like this?

session.headers['username'] = 'abcd'
session.headers['password'] = 'secret'

So far I am getting: #<Patron::Response @status_line='HTTP/1.1 401 Unauthorized'>

When I try to browse using the browser the user name and password is valid.

For example:

Rest Client

RestClient.post 'https://example.com/token/grant', 
  {"app_key":"my_app_key","app_secret":"my_app_secret"}.to_json,  
  {username: 'username', password: 'password', content_type: 'application/json'}

Curb

require 'curb'
request_path= 'https://example.com/token/grant'
payload = {
          app_key: 'app_key',
          app_secret: 'app_secret'
        }

http = Curl.post(request_path, payload.to_json) do |http|
          http.headers['username'] = 'user_name'
          http.headers['password'] = 'password'
          http.headers['Content-Type'] = 'application/json'
        end
JSON.parse(http.body_str)
SaimonL commented 3 years ago

Got it its

session.username = 'abcd'
session.password = 'secret'

the document needs to be better

toland commented 3 years ago

I would be happy to merge a PR that improves the documentation.