I have a custom class with method toJSON and toString which declare how transform instance to JSON string.
For example
class A {
constructor(a, b) {
this.a = a;
this.b = b;
}
toJSON = () => `${this.a} - ${this.b}`
toString = () => `${this.a} - ${this.b}`
}
But when I send instance of class A via axios it sends this instance like "{'a': 'valueA', 'b': 'valueB'}" instead of string as: "valueA - valueB".
Can I use some custom function inside the class which will be used by axios to transform instance to JSON?
Try sending instanceOfA.toString() in the axios call instead of providing instanceOfAas the value to sent as JSON - which axios is sending correctly.
You should probably remove the toJason method of A as well - it won't produce valid JSON text in most cases.