JavaScript · API · Beginner
Headers and the Echo Endpoint
Add custom HTTP headers to requests and use the echo endpoint to inspect what you send.
Quick topic start and explanations before exercises (exercises below):
Using the echo endpoint
#Headers and echo reference
#Exercises:
Send a custom header
#Send a GET request to `/api/echo/` with a custom header `X-My-Name: YourName`. Print the value of that header from the echo response.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// GET /api/echo/ with X-My-Name header
// print the echoed header value
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const response = await fetch(`${BASE_URL}/api/echo/`, {
headers: { 'X-My-Name': 'Alice' },
});
const data = await response.json();
console.log(data.headers['X-My-Name']);
}
main();
Send multiple headers
#Send GET `/api/echo/` with three headers: `X-App: my-app`, `X-Version: 1.0`, and `X-Request-ID: 42`. Print the full `headers` object from the response.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// send 3 custom headers, print data.headers
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const response = await fetch(`${BASE_URL}/api/echo/`, {
headers: {
'X-App': 'my-app',
'X-Version': '1.0',
'X-Request-ID': '42',
},
});
const data = await response.json();
console.log(data.headers);
}
main();
Inspect method and query params via echo
#Send GET `/api/echo/?color=blue&size=large`. Print the method and the `query_params` object from the response.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// GET /api/echo/ with two query params
// print method and query_params
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const params = new URLSearchParams({ color: 'blue', size: 'large' });
const response = await fetch(`${BASE_URL}/api/echo/?${params}`);
const data = await response.json();
console.log(data.method);
console.log(data.query_params);
}
main();
POST with Content-Type header
#Send POST to `/api/echo/` with a JSON body `{name: "test"}` and the `Content-Type: application/json` header. Print the echoed `Content-Type` header and the `body` field.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// POST /api/echo/ with JSON body and Content-Type header
// print the content-type header and body from response
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const response = await fetch(`${BASE_URL}/api/echo/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'test' }),
});
const data = await response.json();
console.log(data.headers['content-type'] ?? data.headers['Content-Type']);
console.log(data.body);
}
main();
Use the Headers class
#Build a `Headers` object with `.set()`, add at least two custom headers, then pass it to a GET `/api/echo/` request. Print the echoed headers.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const headers = new Headers();
// add headers with .set()
// fetch and print echoed headers
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const headers = new Headers();
headers.set('X-Client', 'js-api-course');
headers.set('X-Version', '2.0');
const response = await fetch(`${BASE_URL}/api/echo/`, { headers });
const data = await response.json();
console.log(data.headers);
}
main();