Comments (8)
Axios v1 encodes nested objects in qs style, not as JSON string.
Unfortunately qs does not support serializing a nested object with an empty structure and there is no specification for this or well-known practice.
import qs from "qs";
const params = {
limit: 100,
address: { city: {} },
details: { name: {} },
};
console.log("query:", qs.stringify(params)); // query: limit=100
However, you can use a simple interceptor to achieve your needs:
axios.interceptors.request.use((config) => {
const { params } = config;
if (params) {
const _params = {};
Object.entries(params).forEach(([key, value]) => {
_params[key] =
value && typeof value === "object" ? JSON.stringify(value) : value;
});
config.params = _params;
}
return config;
});
from axios.
Object-type params like address and details are not passed down to the server when sent as a plain object. However, it works fine when using URLSearchParams()
Modify the code to use URLSearchParams() for handling query parameters. This ensures proper encoding, including empty objects. Here's an example:
const params = { limit: 100, address: { city: {} }, details: { name: {} } };
const queryString = new URLSearchParams(params).toString();
axios.get(`/url?${queryString}`)
.then(response => {
// Handle the response
})
.catch(error => {
// Handle the error
});
from axios.
or can you share the reference to the file?
i can take a second look
from axios.
Object-type params like address and details are not passed down to the server when sent as a plain object. However, it works fine when using URLSearchParams() Modify the code to use URLSearchParams() for handling query parameters. This ensures proper encoding, including empty objects. Here's an example:
const params = { limit: 100, address: { city: {} }, details: { name: {} } }; const queryString = new URLSearchParams(params).toString(); axios.get(`/url?${queryString}`) .then(response => { // Handle the response }) .catch(error => { // Handle the error });
These are working fine in 0.24.0 version, it is not working when upgraded to 1.6.0 & it is also not mentioned anything related in the docs.
from axios.
Thanks, Implemented similar.
if (config.params) {
const configParams = config.params;
delete config.params;
const params = new URLSearchParams();
Object.entries(configParams).forEach((param) => {
const [key, value] = param;
if (value !== null && value !== undefined) {
const val = typeof value === 'object' ? JSON.stringify(value) : value;
params.set(key, val);
}
});
config.params = params;
}
from axios.
@DigitalBrainJS facing the same issue, #5986
any alternative to the mentioned issue?
from axios.
I had the same problem and I didn't find any documentation on how the parameters were actually converted. Why does axios v1 not turn them into JSON anymore?
It converts {limit: 100, address: { city: "a" }, details: { name: "b" }}
to limit=100&address[city]=a&details[name]=b
now.
from axios.
axios.interceptors.request.use((config) => { const { params } = config; if (params) { const _params = {}; Object.entries(params).forEach(([key, value]) => { _params[key] = value && typeof value === "object" ? JSON.stringify(value) : value; }); config.params = _params; } return config; });
Could you also use the paramsSerializer
property in the config object? Why do you use an interceptor? It looks like paramsSerializer
exists exactly for this purpose. But I'm not sure how it works exactly.
What's the purpose of this conditional expression value && typeof value === "object" ? JSON.stringify(value) : value
? I think plain JSON.stringify(value)
should work as well.
from axios.
Related Issues (20)
- CSRF Token Mismatch HOT 4
- [fatal][nodejs] “Error: Unexpected end of form” after multiple post file formdata, and then hang up after multiple error happening HOT 3
- "Class extends value #<newConstructor> is not a constructor or null" when sending request from node.js 20.12.0
- [0.28.1] options.paramsSerializer as function don't work HOT 2
- vite5+vue3+ts Sending request error SyntaxError: The requested module '/node_modules/.vite/deps/axios.js?v=bc297318' does not provide an export named 'AxiosInstance' HOT 1
- Issue CVEs for vulnerabilities fixed by #6167 and #6163 HOT 1
- How to set localAddress when sending HTTP requests? HOT 2
- API call happening twice, but called once! HOT 3
- RefferenceError: Axios is not defined. HOT 2
- Security vulnerability HOT 2
- postForm's form-data serialization breaks for Blob in Node.js HOT 1
- Axios stream aborts early during 204 response from prism cli.
- I am getting a error involving CancelToken.js...
- TypeError: Converting circular structure to JSON HOT 2
- React native Android NETWORK ERROR HOT 1
- Issue with downloading files larger than a few mbs on Chrome specifically HOT 4
- please expose the formDataToStream for public use
- 100% Upload progress on dev mode only.
- XSRF-TOKEN logic should utilize cookie domain instead of current page domain HOT 1
- Question: Rollup configuration with ESM
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from axios.