Quering json data from Foreman API using javascript

Hello,
So, I am trying to query discovered hosts from Foreman API at endpoint https://(my foreman)/api/v2/discovered_hosts, using javascript but, everytime I try it, it gives me 401(unauthorized) error.
If I try to pass the username and psswd I use to get to foreman on my machibe, it still gives me the same error. Even curl request is throwing me an error of “ssl certificate problem”. How do I go about fixing this? Can I generate some token to access the api.
Any help would be highly appreciated!