Need to call import classes API using session

I need to call Foreman’s API using session authentication. I was getting the error “Unable to authenticate”.

Foreman and Proxy versions: Version 1.24.3

Hey @rishika

Welcome to the community,

Can you provide some more information and examples of exactly what you were doing when you ran into trouble? What session authentication?

If you can give us more detail, we’ll have a better idea how to help you.

Hey @mcorr

Thanks for replying.
I need to import puppet classes via api.
Using the following command:
url = “https://localhost/api/smart_proxies/1/import_puppetclasses
uri = URI(url)
http =, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request =
response = http.request(request)
Getting unable to authenticate error.
How can we call foreman’s api from ruby?

Hi @rishika,

well as I can see there is no authentication within your code, but the API endpoint you want to call requires it.

The easiest way is to have something like:

request.basic_auth(user, password) 

But this is not recommended since you have your password stored in plain way in script.

Can I ask why don’t you use hammer CLI for such purpose? Hammer supports sessions, so you don’t have to write your credentials all the time.

1 Like

Hi @ofedoren ,

We are already using hammer but we want to call it from UI of foreman on a button click from RUBY.
I am using this link
How to call these api by not using username and password ?

@rishika, can you please elaborate

call it from UI of foreman on a button click from RUBY

Do you mean you’re writing a plugin that have a button in UI which onclick will call that endpoint? Because I don’t quite follow what do you mean by button click from Ruby (maybe you mean some .html.erb file?)

@ofedoren ,

Yes I am writing a plugin where I am developing a view(file.html.erb) here on a button click I need to import some classes and then call this api.


well, there is already view with the logic of importing puppet classes [1]. So, you might take a look how it’s done there. Note that you don’t need to do authentication in views since you can interact with those pages only after you log in as a user. Basically it’s a controller with the main logic and a view for interacting with it, auth-stuff is done elsewhere.

But if you want to use the API then it’s better to not use Rails views, but React UI, which is supported for plugins to extend.

[1] - foreman/index.html.erb at 2bb8a0edda627c65fe3347091fbd95e86835d244 · theforeman/foreman · GitHub

1 Like