2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-17 19:21:29 +03:00
Files
axios/lib/defaults/index.js
T
Pierluigi Lenoci f39203dcbe feat: add QUERY HTTP method support (#10802)
* feat: add QUERY HTTP method support

Add support for the HTTP QUERY method as defined in
draft-ietf-httpbis-safe-method-w-body. QUERY is a safe, idempotent
method like GET but carries a request body, making it suitable for
complex queries that cannot be expressed in a URL.

Changes:
- Add axios.query(url, data, config) and axios.queryForm() shorthands
- Register 'query' in default headers initialization
- Include 'query' in header cleanup during request dispatch
- Add 'QUERY' to Method type in TypeScript definitions (d.ts and d.cts)
- Add query/queryForm signatures to Axios class type definitions
- Add 'query' to HeadersDefaults interface
- Update unit and module typing tests

Closes #5465

Signed-off-by: Pierluigi Lenoci <pierluigilenoci@gmail.com>

* test: add thorough QUERY method tests

Add comprehensive tests for the QUERY HTTP method covering:
- Request method correctness (via mock adapter and real HTTP server)
- Request body support (QUERY accepts a body like POST/PUT/PATCH)
- Custom headers handling
- baseURL configuration with instances
- Content-Type auto-detection (application/json for objects)
- Instance method and defaults merging
- Generic request form axios({ method: 'query' })
- queryForm() multipart/form-data support
- Integration tests against a real HTTP server verifying the QUERY
  method string arrives correctly on the wire

Signed-off-by: Pierluigi Lenoci <pierluigilenoci@gmail.com>

* chore: updated docs with all translations

* chore: drop formquery method as this is probably not a real use case

* chore: remove un-needed file

---------

Signed-off-by: Pierluigi Lenoci <pierluigilenoci@gmail.com>
Co-authored-by: Jay <jasonsaayman@gmail.com>
2026-04-28 14:28:30 +02:00

178 lines
4.9 KiB
JavaScript

'use strict';
import utils from '../utils.js';
import AxiosError from '../core/AxiosError.js';
import transitionalDefaults from './transitional.js';
import toFormData from '../helpers/toFormData.js';
import toURLEncodedForm from '../helpers/toURLEncodedForm.js';
import platform from '../platform/index.js';
import formDataToJSON from '../helpers/formDataToJSON.js';
const own = (obj, key) => (obj != null && utils.hasOwnProp(obj, key) ? obj[key] : undefined);
/**
* It takes a string, tries to parse it, and if it fails, it returns the stringified version
* of the input
*
* @param {any} rawValue - The value to be stringified.
* @param {Function} parser - A function that parses a string into a JavaScript object.
* @param {Function} encoder - A function that takes a value and returns a string.
*
* @returns {string} A stringified version of the rawValue.
*/
function stringifySafely(rawValue, parser, encoder) {
if (utils.isString(rawValue)) {
try {
(parser || JSON.parse)(rawValue);
return utils.trim(rawValue);
} catch (e) {
if (e.name !== 'SyntaxError') {
throw e;
}
}
}
return (encoder || JSON.stringify)(rawValue);
}
const defaults = {
transitional: transitionalDefaults,
adapter: ['xhr', 'http', 'fetch'],
transformRequest: [
function transformRequest(data, headers) {
const contentType = headers.getContentType() || '';
const hasJSONContentType = contentType.indexOf('application/json') > -1;
const isObjectPayload = utils.isObject(data);
if (isObjectPayload && utils.isHTMLForm(data)) {
data = new FormData(data);
}
const isFormData = utils.isFormData(data);
if (isFormData) {
return hasJSONContentType ? JSON.stringify(formDataToJSON(data)) : data;
}
if (
utils.isArrayBuffer(data) ||
utils.isBuffer(data) ||
utils.isStream(data) ||
utils.isFile(data) ||
utils.isBlob(data) ||
utils.isReadableStream(data)
) {
return data;
}
if (utils.isArrayBufferView(data)) {
return data.buffer;
}
if (utils.isURLSearchParams(data)) {
headers.setContentType('application/x-www-form-urlencoded;charset=utf-8', false);
return data.toString();
}
let isFileList;
if (isObjectPayload) {
const formSerializer = own(this, 'formSerializer');
if (contentType.indexOf('application/x-www-form-urlencoded') > -1) {
return toURLEncodedForm(data, formSerializer).toString();
}
if (
(isFileList = utils.isFileList(data)) ||
contentType.indexOf('multipart/form-data') > -1
) {
const env = own(this, 'env');
const _FormData = env && env.FormData;
return toFormData(
isFileList ? { 'files[]': data } : data,
_FormData && new _FormData(),
formSerializer
);
}
}
if (isObjectPayload || hasJSONContentType) {
headers.setContentType('application/json', false);
return stringifySafely(data);
}
return data;
},
],
transformResponse: [
function transformResponse(data) {
const transitional = own(this, 'transitional') || defaults.transitional;
const forcedJSONParsing = transitional && transitional.forcedJSONParsing;
const responseType = own(this, 'responseType');
const JSONRequested = responseType === 'json';
if (utils.isResponse(data) || utils.isReadableStream(data)) {
return data;
}
if (
data &&
utils.isString(data) &&
((forcedJSONParsing && !responseType) || JSONRequested)
) {
const silentJSONParsing = transitional && transitional.silentJSONParsing;
const strictJSONParsing = !silentJSONParsing && JSONRequested;
try {
return JSON.parse(data, own(this, 'parseReviver'));
} catch (e) {
if (strictJSONParsing) {
if (e.name === 'SyntaxError') {
throw AxiosError.from(e, AxiosError.ERR_BAD_RESPONSE, this, null, own(this, 'response'));
}
throw e;
}
}
}
return data;
},
],
/**
* A timeout in milliseconds to abort a request. If set to 0 (default) a
* timeout is not created.
*/
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
env: {
FormData: platform.classes.FormData,
Blob: platform.classes.Blob,
},
validateStatus: function validateStatus(status) {
return status >= 200 && status < 300;
},
headers: {
common: {
Accept: 'application/json, text/plain, */*',
'Content-Type': undefined,
},
},
};
utils.forEach(['delete', 'get', 'head', 'post', 'put', 'patch', 'query'], (method) => {
defaults.headers[method] = {};
});
export default defaults;