0
0
Rest APIprogramming~20 mins

Self link for current resource in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
REST API Self Link Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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))
A"http://localhost/items/42"
B"/items/42"
C"http://localhost/items?id=42"
D"http://localhost/items/"
Attempts:
2 left
💡 Hint
Look at the _external=True parameter in url_for to understand the full URL generation.
🧠 Conceptual
intermediate
1: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?
AContent-Location
BLocation
CLink
DSelf
Attempts:
2 left
💡 Hint
Think about headers that can contain multiple URLs with relation types.
🔧 Debug
advanced
2: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 });
});
Areq.protocol is undefined in Express, causing an error
BThe template string syntax is incorrect
Creq.params.id is not accessible in this route
Dreq.hostname does not include the port, so the URL may be incomplete
Attempts:
2 left
💡 Hint
Check how Express provides host and port information in requests.
📝 Syntax
advanced
2: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
}
Areturn EntityModel.of(book, linkTo(methodOn(BookController.class).getBook(id)).withSelfRel());
Breturn new EntityModel<>(book, new Link("/books/" + id));
Creturn ResponseEntity.ok(book).header("self", "/books/" + id).build();
Dreturn book.add(linkTo(BookController.class).slash(id).withSelfRel());
Attempts:
2 left
💡 Hint
Look for the standard way to create self links with Spring HATEOAS using methodOn and linkTo.
🚀 Application
expert
1: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?
A3
B4
C5
D6
Attempts:
2 left
💡 Hint
Count the keys directly inside the outermost curly braces.