Challenge - 5 Problems
REST API Self Link Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this REST API self link generation code?
Given the following Python Flask code snippet that generates a self link for a resource, what is the output when accessing the endpoint /items/42?
Rest API
from flask import Flask, url_for, jsonify, request app = Flask(__name__) @app.route('/items/<int:item_id>') def get_item(item_id): self_url = url_for('get_item', item_id=item_id, _external=True) return jsonify({'id': item_id, 'self': self_url}) with app.test_request_context(): print(url_for('get_item', item_id=42, _external=True))
Attempts:
2 left
💡 Hint
Look at the _external=True parameter in url_for to understand the full URL generation.
✗ Incorrect
The url_for function with _external=True generates the full URL including the scheme and host. The route uses a path parameter, so the URL includes /items/42.
🧠 Conceptual
intermediate1:30remaining
Which HTTP header is commonly used to indicate the self link of a resource in REST APIs?
In REST API design, which HTTP header is typically used to provide the URL of the current resource (self link) in the response headers?
Attempts:
2 left
💡 Hint
Think about headers that can contain multiple URLs with relation types.
✗ Incorrect
The Link header can include URLs with relation types such as rel="self" to indicate the self link of the resource.
🔧 Debug
advanced2:30remaining
Why does this code fail to generate a correct self link?
Consider this Node.js Express code snippet intended to add a self link to a JSON response. Why does it fail to produce the correct full URL for the self link?
Rest API
app.get('/users/:id', (req, res) => { const selfLink = `${req.protocol}://${req.hostname}/users/${req.params.id}`; res.json({ id: req.params.id, self: selfLink }); });
Attempts:
2 left
💡 Hint
Check how Express provides host and port information in requests.
✗ Incorrect
req.hostname returns only the hostname without the port. If the server runs on a non-default port, the generated URL misses the port, making the self link incomplete.
📝 Syntax
advanced2:00remaining
Which option correctly adds a self link in a JSON response in Java Spring Boot?
Given a Spring Boot controller method, which code snippet correctly adds a self link to the returned resource using Spring HATEOAS?
Rest API
@GetMapping("/books/{id}")
public EntityModel<Book> getBook(@PathVariable Long id) {
Book book = bookRepository.findById(id).orElseThrow();
// Add self link here
}Attempts:
2 left
💡 Hint
Look for the standard way to create self links with Spring HATEOAS using methodOn and linkTo.
✗ Incorrect
Option A uses EntityModel.of with linkTo and methodOn to create a self link properly. Other options either misuse classes or headers.
🚀 Application
expert1:30remaining
How many items are in the resulting JSON when adding a self link to a nested resource?
Consider this JSON response for a REST API resource with a nested subresource and self links added at both levels:
{
"id": 10,
"name": "Folder",
"self": "http://api.example.com/folders/10",
"files": [
{
"id": 5,
"filename": "doc.txt",
"self": "http://api.example.com/folders/10/files/5"
}
]
}
How many total keys are at the top level of the JSON object?
Attempts:
2 left
💡 Hint
Count the keys directly inside the outermost curly braces.
✗ Incorrect
The top-level keys are: id, name, self, and files. That makes 4 keys total.