Junior
Is it possible to pass data using a GET request?
sobes.tech AI
Answer from AI
Yes, it is possible.
Data is transmitted as parameters in the query string, appended to the URI after the ? symbol. Each parameter is a pair of "key=value", separated by the & symbol.
For example:
/users?id=123&status=active
Features and limitations of GET requests for data transmission:
- Data visibility: Parameters are visible in the URL, making them unsuitable for transmitting confidential information.
- URL length limit: Most browsers and servers have a maximum URL length limit, restricting the amount of data transmitted.
- Cacheability: Responses to GET requests can be cached.
- Idempotency: Performing the same GET request multiple times should have the same effect as doing it once. It should not change the server's state.
- Usage: Intended for retrieving data, not for sending data to change the server's state.
// Example of retrieving GET request parameters in Node.js using Express
const express = require('express');
const app = express();
app.get('/users', (req, res) => {
const userId = req.query.id; // Getting the 'id' parameter
const userStatus = req.query.status; // Getting the 'status' parameter
console.log(`User ID: ${userId}, Status: ${userStatus}`);
res.send(`Received data: ID=${userId}, Status=${userStatus}`);
});
app.listen(3000, () => {
console.log('Server listening on port 3000');
});