How do I do basic authentication with RestClient?

Viewed 49968

Does anyone know how to do basic authentication with RestClient?

I need to create a private repository on GitHub through their RESTful API.

5 Answers

This works and follows RFC 7617 for Http Basic Authentication:


RestClient::Request.execute(
  method: :post,
  url: "https://example.com",
  headers: { "Authorization" => "Basic " + Base64::encode64(auth_details) },
  payload: { "foo" => "bar"}
)


def auth_details
  ENV.fetch("HTTP_AUTH_USERNAME") + ":" + ENV.fetch("HTTP_AUTH_PASSWORD")
end

Thanks to Kelsey Hannan:

RestClient.get("https://example.com", 
  {
    Authorization: "Basic #{Base64::encode64('guest:guest')}"
  }
)

RestClient.post("https://example.com", 
  {  }.to_json,
  {
    Authorization: "Basic #{Base64::encode64('guest:guest')}"
  }
)
Related