Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AxiosError: options must be an object #5142

Closed
ckvv opened this issue Oct 17, 2022 · 14 comments
Closed

AxiosError: options must be an object #5142

ckvv opened this issue Oct 17, 2022 · 14 comments

Comments

@ckvv
Copy link

ckvv commented Oct 17, 2022

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

@J3m5
Copy link

J3m5 commented Oct 17, 2022

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

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

Should fix it

@shomyx
Copy link

shomyx commented Oct 17, 2022

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.

@DigitalBrainJS
Copy link
Collaborator

DigitalBrainJS commented Oct 17, 2022

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);
})();

@J3m5
Copy link

J3m5 commented Oct 17, 2022

This is the expected format from the assertOptions function

@Roriz
Copy link

Roriz commented Oct 17, 2022

Is issue has origin on new feature: #5113
We should change a major version for that please

@J3m5
Copy link

J3m5 commented Oct 17, 2022

Is issue has origin on new feature: #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.

@DigitalBrainJS
Copy link
Collaborator

DigitalBrainJS commented Oct 17, 2022

at least before a new major version is released.

That's exactly what was done. This interface has been refactored in 1.0.0-alpha.1

@J3m5
Copy link

J3m5 commented Oct 17, 2022

That's exactly what was done. This interface has been refactored in 1.0.0-alpha.1

I'll double check the timing but I've been using this interface before v1 and it just broke with 1.1.3 today.
I'm guessing that it was silently failing and it is now throwing an error and thus breaking.

@philBrown
Copy link

Duplicate of axios/axios-docs#90

@shomyx
Copy link

shomyx commented Oct 18, 2022

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.

@ckvv
Copy link
Author

ckvv commented Oct 18, 2022

const serializeFn = options && options.serialize;

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

@arpitha-jaanaki
Copy link

arpitha-jaanaki commented Feb 9, 2023

HI
i just upgraded axios version to 1..3.2 and facing error like "options must be an object"
tried above all the solutions nothing worked for me,

**"axios": "^1.3.2",**

here my axiosservice.js file looks like

import axios, { isCancel, AxiosError } from 'axios';
import { qs, parse, stringify } from 'qs';

class AxiosService {
	instance = axios.create({
		paramsSerializer: {
			serialize: (params) => Qs.stringify(params, { arrayFormat: 'brackets' })
		}
	});
	
	requestInterceptor = this.instance.interceptors.request.use(
		(config) => {
			let accessToken = localStorage.getItem(SESSION_CONSTANTS.ACCESS_TOKEN);
			config.headers[HTTP_HEADERS.AUTHORIZATION] = `bearer ${accessToken}`;
			config.headers[HTTP_HEADERS.X_AUTH_TOKEN] = `${accessToken}`;
			config.headers[HTTP_HEADERS.REQUEST_FROM] = `GUI`;
			return config;
		},
		(err) => Promise.reject(err)
	);

anyway im missing anything here ?

also tried below solution too

class AxiosService {
	instance = axios.create({
		paramsSerializer: {
			encode: parse,
			serialize: stringify
		}
	});

@murbanowicz
Copy link

paramsSerializer: {
			encode: parse,
			serialize: stringify
		}

working, but causing TypeScript issues. Any idea how to work around it?

@naftalimurgor
Copy link

Hi @murbanowicz
Have you been able to resolve this issue? Got a similar issue with TypeScript

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants