Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
Axios sends custom object with wrong JSON transformation

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!