0

we have this object, we need to get it as JSON instead of JS object, so we do it with JSON.stringify() and we receive this error:

Converting circular structure to JSON
>      --> starting at object with constructor 'ClientRequest'
>      |     property 'socket' -> object with constructor 'TLSSocket'
>      --- property '_httpMessage' closes the circle

and here is my whole end-point, I think it might have something to do with Axios, please note we uses TypeScript so actually this file is not .js file but .ts:

  app.post('/payment-key-request', async (req: any, res: any) => {
  const {auth_token, order_id} = req.body;
  const payload1 = {
    auth_token,
    amount_cents: 100,
    expiration: 3600,
    order_id,
    billing_data: {
      apartment: 803,
      email: "claudette09@exa.com",
      floor: 42,
      first_name: "Clifford",
      street: "Ethan Land",
      building: 8028,
      phone_number: 9135210487,
      shipping_method: "PKG",
      postal_code: 41523,
      city: "Jaskolskiburgh",
      country: "CR",
      last_name: "Nicolas",
      state: "Utah"
    },
    currency: "EGP",
    integration_id: 1 // something goes off here
  };


    let cache: any = [];
await Axios.post(
    "https://accept.paymobsolutions.com/api/acceptance/payment_keys",
    JSON.stringify(payload1),
    {
      headers: { "Content-Type": "application/json" }
    }
  )
    .then((resp: any) => {
      res.status(200).json({ ...resp });
    })
    .catch((error: any) => {
      if (error.response) {
        console.log("// Request made and server responded");
        console.log(error.response.data);
        console.log(error.response.status);
        console.log(error.response.headers);
      } else if (error.request) {
        console.log("// The request was made but no response was received");
        console.log(error.request);
      } else {
        console.log(
          "// Something happened in setting up the request that triggered an Error"
        );
        console.log(error.message);
      }
      res.status(400).json({ ...error });
    });

    cache = null;
})
kyrolos magdy
  • 393
  • 1
  • 4
  • 19
  • Taking your object copy and paste and attempting `JSON.stringify(payload1)` I first got "auth_token" is not defined, then "order_Id" is not defined. I added values to those properties and stringify worked fine. Not clear why you get Circular structure error from what you've provided. – daddygames Jul 27 '20 at 14:28
  • So, you think it's not about .stringify, right? – kyrolos magdy Jul 27 '20 at 14:29
  • It's a malformed object for JSON. Just not clear what is malformed at this point other than the 2 empty properties. – daddygames Jul 27 '20 at 14:31
  • They are not empty, it's an end-point that recivievs the auth_toen, oreder_id as in req.body, I will share the whole end point with you – kyrolos magdy Jul 27 '20 at 14:35
  • Does this answer your question? [How can I print a circular structure in a JSON-like format?](https://stackoverflow.com/questions/11616630/how-can-i-print-a-circular-structure-in-a-json-like-format) – Thomas Jul 27 '20 at 14:35
  • No, will edit the question now – kyrolos magdy Jul 27 '20 at 14:43

1 Answers1

1

Instead of trying to respond with the entire response object, you should send just the parsed body:

res.status(200).json(resp.data);

Same with the error case:

res.status(400).json(error.response.data);
Ry-
  • 218,210
  • 55
  • 464
  • 476