axios: AxiosError: options must be an object

Seems to be because the config created by create is not being merged correctly

Describe the bug

F {message: 'options must be an object', name: 'AxiosError', code: 'ERR_BAD_OPTION_VALUE', stack: 'AxiosError: options must be an object\n    at Objec…le:///Users/chenkai/Desktop/test/index.html:21:28'}
code
: 
"ERR_BAD_OPTION_VALUE"
message
: 
"options must be an object"
name
: 
"AxiosError"
stack
: 
"AxiosError: options must be an object\n    at Object.Je [as assertOptions] (https://esm.sh/v96/axios@1.1.3/es2022/axios.js:3:10223)\n    at B.request (https://esm.sh/v96/axios@1.1.3/es2022/axios.js:3:10983)\n    at B.<computed> [as get] (https://esm.sh/v96/axios@1.1.3/es2022/axios.js:3:12121)\n    at Function.get (https://esm.sh/v96/axios@1.1.3/es2022/axios.js:2:148)\n    at file:///Users/chenkai/Desktop/test/index.html:21:28"
[[Prototype]]
: 
Error

To Reproduce

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
</body>
<script type="module">
import Axios from 'https://esm.sh/axios';
import Qs from 'https://esm.sh/qs@6.11.0';

const instance = Axios.create({
  paramsSerializer(params) {
    return Qs.stringify(params, {arrayFormat: 'brackets'})
  },
});

const res = await instance.get('https://jsonplaceholder.typicode.com/comments');
console.log(res);
</script>
</html>

Code snippet

No response

Expected behavior

No response

Axios Version

v1.1.3

Adapter Version

No response

Browser

No response

Browser Version

No response

Node.js Version

No response

OS

No response

Additional Library Versions

No response

Additional context/Screenshots

No response

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 9
  • Comments: 15

Commits related to this issue

Most upvoted comments

https://github.com/axios/axios/blob/9d4b016ddedf835bfd838b63e50af14d84e2813e/lib/helpers/buildURL.js#L47

The original spelling should be changed to

const instance = Axios.create({
  paramsSerializer: {
    serialize: (params) => Qs.stringify(params, {arrayFormat: 'brackets'})
  },
});

This is a breaking update, but the documentation doesn’t say ,and it contains a previous wrong usage

It seems to work as expected https://codesandbox.io/s/axios-issue-5142-92zlpm?file=/src/index.js

import axios from "axios";
import { stringify } from "qs";

const api = axios.create({
  paramsSerializer: {
    serialize: stringify // or (params) => Qs.stringify(params, {arrayFormat: 'brackets'})
  }
});

const formData = new FormData();
formData.append("file", 123);

(async () => {
  const { data } = await api.post("https://httpbin.org/post", formData, {
    params: {
      x: [1, 2, 3]
    }
  });

  console.log("Echo:", data);
})();

import axios from 'axios'
import { parse, stringify } from 'qs'

const axiosInstance = axios.create({
  paramsSerializer: {
    encode: parse,
    serialize: stringify,
  },
})

Should fix it

Same issue as @ckvv , but solution above did not fix it for me. Everything still the same.

It’s working after all, just had to tweak something in a custom middleware I had. Thanks @J3m5 for the tip.

import axios from 'axios'
import { parse, stringify } from 'qs'

const axiosInstance = axios.create({
  paramsSerializer: {
    encode: parse,
    serialize: stringify,
  },
})

Should fix it

Is issue has origin on new feature: https://github.com/axios/axios/pull/5113 We should change a major version for that please

This is clearly a breaking change, documentation should also be updated accordingly. The old format should be declared as deprecated but still be available to use, at least before a new major version is released.

Is issue has origin on new feature: https://github.com/axios/axios/pull/5113 We should change a major version for that please