JavaScript Example
How to consume an API using JavaScript
// Using fetch API
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => {
console.log('Data:', data)
})
.catch(error => {
console.error('Error:', error)
})
Python Example
How to consume an API using Python
import requests
response = requests.get('https://api.example.com/data')
if response.status_code == 200:
data = response.json()
print('Data:', data)
else:
print('Error:', response.status_code)
Go Example
How to consume an API using Go
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
resp, err := http.Get("https://api.example.com/data")
if err != nil {
fmt.Println("Error:", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Data:", string(body))
}