Overall problem: How to do a simple REST API call in javascript when a button is clicked
I want to do the equivalent of this REST call but in javascript
curl -X GET \
https://www.mydog.com/api/v3/user \
-H 'Authorization: Bearer 1234'
Attempted solution that doesn't work
page.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Submit</title>
</head>
<body>
<script src="submit.js"></script>
<button onclick="apicall()">Make REST API call</button>
</body>
</html>
submit.js
const axios = require('axios').default;
function apicall() {
console.log("apicall called");
const headers = {
"Authorization": "Bearer 1234",
}
axios.get(
"https://www.mydog.com/api/v3/user",
{headers: headers}
).then(function (response){
console.log(response)
}).catch(function (error) {
// handle error
console.log(error);
}).then(function () {
// always executed
});
Problem with attempted solution
Opening page.html and clicking the button shows this in the console: submit.js:1 Uncaught ReferenceError: require is not defined
Answer to subproblem (but feels not straightforward)
Client on Node.js: Uncaught ReferenceError: require is not defined
Never doing web programming before (but many years in algorithm / applied math type of programming) I'm wondering if I'm getting off track. This answer seems helpful except isn't making an http request something simple that's built into every language? Should I be using something beside axios maybe? What's the simplest way to do a client javascript side http call? Or am I asking the wrong question and need to do backend javascript for this?