axios/axios · error · TypeError

target must be an object

Error message

target must be an object

What it means

`toFormData` — the helper axios uses to serialize a plain object into FormData (e.g. for multipart/form-data requests or via `axios.toFormData`) — requires its first argument to be an object. If `utils.isObject(obj)` fails, it throws a plain TypeError 'target must be an object' before doing any work. Note this is one of the few raw TypeErrors in the library (it's an argument-contract violation, not an axios-originated request failure).

Source

Thrown at lib/helpers/toFormData.js:96

 * @param {Boolean} [options.metaTokens = true]
 * @param {Boolean} [options.dots = false]
 * @param {?Boolean} [options.indexes = false]
 *
 * @returns {Object}
 **/

/**
 * It converts an object into a FormData object
 *
 * @param {Object<any, any>} obj - The object to convert to form data.
 * @param {string} formData - The FormData object to append to.
 * @param {Object<string, any>} options
 *
 * @returns
 */
function toFormData(obj, formData, options) {
  if (!utils.isObject(obj)) {
    throw new TypeError('target must be an object');
  }

  // eslint-disable-next-line no-param-reassign
  formData = formData || new (PlatformFormData || FormData)();

  // eslint-disable-next-line no-param-reassign
  options = utils.toFlatObject(
    options,
    {
      metaTokens: true,
      dots: false,
      indexes: false,
    },
    false,
    function defined(option, source) {
      // eslint-disable-next-line no-eq-null,eqeqeq
      return !utils.isUndefined(source[option]);
    }

View on GitHub (pinned to c3f553c740)

Solutions

  1. Pass a plain object (or array) as the data: `axios.post(url, { name: 'x', file }, { headers: { 'Content-Type': 'multipart/form-data' } })`.
  2. Don't pre-serialize — remove JSON.stringify/querystring encoding and let axios serialize, or drop the multipart Content-Type if you intend to send a raw string.
  3. If you already have a FormData instance, pass it directly as the request data instead of running it through toFormData.

Example fix

// before
await axios.post('/upload', JSON.stringify(payload), { headers: { 'Content-Type': 'multipart/form-data' } });
// after
await axios.post('/upload', payload, { headers: { 'Content-Type': 'multipart/form-data' } });
Defensive patterns

Strategy: type-guard

Validate before calling

if (payload === null || typeof payload !== 'object' || Array.isArray(payload)) {
  throw new TypeError('toFormData payload must be a plain object or FormData-compatible object');
}

Type guard

function isFormDataTarget(value) {
  return value !== null && typeof value === 'object';
}

Try / catch

try {
  const fd = axios.toFormData(payload, new FormData());
} catch (err) {
  if (err instanceof TypeError && /target must be an object/.test(err.message)) {
    // caller passed a primitive/null as the FormData target
  } else throw err;
}

Prevention

When it happens

Trigger: Calling `axios.toFormData(value)` or posting with a multipart Content-Type where the request data is a string, number, null, undefined, or boolean instead of an object/array — e.g. `axios.post(url, 'name=x', { headers: { 'Content-Type': 'multipart/form-data' } })`.

Common situations: Passing an already-serialized string (JSON.stringify'd data or a querystring) while also setting a multipart Content-Type, which routes it through toFormData; passing undefined because a variable wasn't populated; confusing URLSearchParams-style usage with FormData usage.

Related errors


AI-assisted analysis of axios/axios@c3f553c740 (2026-07-31). Data as JSON: /data/errors/11901925c035011d.json. Report an issue: GitHub.