Axios Default HTTP Method for Requests
When making a network call using the Axios HTTP client without
explicitly specifying an HTTP method, Axios automatically defaults to
sending a GET request. This article explains how Axios
handles unspecified methods in its configuration object, how the
internal default settings resolve these requests, and how developers can
leverage or modify this default behavior in their applications.
The Default Method: GET
In Axios, if you invoke the library directly by passing a URL string
or a configuration object without defining the method
property, the client treats the operation as an HTTP GET
request.
For example, both of the following implementations perform an
identical GET request:
// Passing a URL string directly
axios('https://api.example.com/users')
.then(response => console.log(response.data));
// Passing a configuration object without a 'method' property
axios({
url: 'https://api.example.com/users'
})
.then(response => console.log(response.data));How Axios Resolves Unspecified Methods Internally
Axios merges user-provided configuration objects with its default
settings before executing a request. Within the Axios core defaults
(axios.defaults), the method property is set
to 'get' by default.
During the request execution pipeline:
- Config Merging: Axios checks if a
methodproperty exists in the request configuration. - Fallback Assignment: If
config.methodisundefined, it adopts the value fromdefaults.method('get'). - Normalization: Axios normalizes the method string
to lowercase before dispatching the request to the underlying adapter
(such as
XMLHttpRequestin browsers or thehttp/httpsmodule in Node.js).
Sending Payloads Without a Defined Method
Because an omitted method defaults to GET, attempting to
pass a payload via the data property without declaring a
method can lead to unexpected behavior:
// This will still execute as a GET request
axios({
url: 'https://api.example.com/users',
data: {
name: 'Jane Doe'
}
});While Axios will transmit the request, many servers and proxies
discard the body of incoming GET requests, or they may
reject the request entirely. For operations requiring a request body
(such as creating or updating resources), you must explicitly specify
POST, PUT, or PATCH.
Changing the Global Default Method
While rare, it is possible to change the global default method for an Axios instance:
// Change default method globally
axios.defaults.method = 'post';
// Creates a custom Axios instance with a different default method
const customClient = axios.create({
baseURL: 'https://api.example.com',
method: 'post'
});Setting an explicit method—either through the method
config property or by using shorthand helper methods like
axios.get() and axios.post()—is generally
recommended to maintain readability and prevent unintentional
GET calls.