This API endpoint provides data in JSON format.
// CJS
// If you are using ESM, remove this line!
const fetch = require('node-fetch');
// ESM
// If you are using CJS, remove this line!
import fetch from 'node-fetch';
// GET /api/data
const getData = async () => (await fetch('http://example.com/api/data')).json();
// POST /api/data
const postData = async (body) =>
(await fetch('http://example.com/api/data', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
})).json();
// Usage examples
getData().then(console.log);
postData({ key: 'value' }).then(console.log);
// CJS
// If you are using ESM, remove this line!
const axios = require('axios');
// ESM
// If you are using CJS, remove this line!
import axios from 'axios';
// GET /api/data
const getData = async () => {
const response = await axios.get('http://example.com/api/data');
return response.data;
};
// POST /api/data
const postData = async (body) => {
const response = await axios.post('http://example.com/api/data', body, {
headers: { 'Content-Type': 'application/json' },
});
return response.data;
};
// Usage examples
getData().then(console.log).catch(console.error);
postData({ key: 'value' }).then(console.log).catch(console.error);
// GET /api/data
function getData() {
$url = 'http://example.com/api/data';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
// POST /api/data
function postData($payload) {
$url = 'http://example.com/api/data';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
// Usage examples
print_r(getData());
print_r(postData(['key' => 'value']));
import requests
# GET /api/data
def get_data():
response = requests.get('http://example.com/api/data')
return response.json()
# POST /api/data
def post_data(payload):
response = requests.post(
'http://example.com/api/data',
json=payload,
headers={'Content-Type': 'application/json'}
)
return response.json()
# Usage examples
print(get_data())
print(post_data({'key': 'value'}))