Axios Params vs Data: What Is the Difference?
When making HTTP requests using Axios, configuring how you send
information to the server is crucial for API communication. Two of the
most common configuration options are params and
data. While both are used to pass information to an
endpoint, they function in completely different ways, attach data to
different parts of the HTTP request, and are intended for distinct HTTP
methods. This guide outlines the core differences, use cases, and syntax
for both options.
What is params in
Axios?
The params property is used to send URL query
parameters. When you specify params, Axios
serializes the key-value pairs and appends them to the end of the
request URL following a question mark (?).
Key Characteristics of
params:
- Location: Appended directly to the URL string
(e.g.,
/users?role=admin&page=2). - HTTP Methods: Primarily used with
GET,DELETE, andHEADrequests where a request body is typically not sent. - Data Types: Plain objects or
URLSearchParamsinstances. - Visibility: Fully visible in the browser address bar, server access logs, and network history.
- Limitations: Size is limited by maximum URL length
constraints enforced by browsers and servers. Sensitive data (like
passwords or tokens) should never be passed via
params.
Example Using params:
axios.get('/api/products', {
params: {
category: 'electronics',
sort: 'price_asc'
}
});
// Resulting Request: GET /api/products?category=electronics&sort=price_ascWhat is data in Axios?
The data property is used to send a request body
payload. Instead of modifying the URL, Axios packages this
information inside the body of the HTTP request.
Key Characteristics of
data:
- Location: Transmitted inside the HTTP message body.
- HTTP Methods: Primarily used with
POST,PUT,PATCH, and occasionallyDELETE. - Data Types: Objects (automatically serialized to
JSON by default), strings,
FormData,Blob,ArrayBuffer, orStream. - Visibility: Not visible in the URL; can only be inspected via network payloads.
- Payload Size: Can handle large datasets, file uploads, and complex nested JSON structures.
Example Using data:
axios.post('/api/products', {
name: 'Wireless Headphones',
price: 99.99,
inStock: true
});
// Resulting Request: POST /api/products
// Request Body: {"name":"Wireless Headphones","price":99.99,"inStock":true}Summary of Differences
| Feature | params |
data |
|---|---|---|
| Location | URL Query String
(?key=value) |
HTTP Request Body |
| Primary HTTP Methods | GET, DELETE,
HEAD |
POST, PUT,
PATCH |
| Content-Type Header | Not applicable | Typically application/json or
multipart/form-data |
| Size Limit | Restricted by URL length limits (~2,048 characters) | Determined by server-configured payload limits (often megabytes) |
| Data Structure | Flat key-value pairs | Complex objects, arrays, files, and text |
| Security | Low (exposed in logs, history, and URL) | Higher (hidden in payload, though HTTPS is still required) |
Choosing the Right Option
- Use
paramswhen you need to filter, paginate, sort, or query resources without altering server state. - Use
datawhen you need to create, update, or send structured resources, sensitive credentials, or file uploads to the server.