1

I need to send an array in the body of a fetch request. The value fields has to be an array with lastName and some other values. How do I go about this? I've tried

         let fields = JSON.stringify({ lastName: "test" }); 

but that did not work. I'm trying to do this in node.js. This is the code:

        const params = new URLSearchParams();

        params.append("name", name);
        params.append("email", email);
        params.append("fields", fields);

        fetch(url, {
          headers: Authorization,
          method: "POST",
          body: params,
        })
          .then((e) => {
            console.log(e);
          })

The request itself works fine, it's just fields that isn't picked up.

Coen Visser
  • 23
  • 1
  • 6
  • 1
    you said you need to send an array. Your example was not an array. Which is it? – Abraham Labkovsky Sep 29 '20 at 18:33
  • Did you try something like. `let fields = JSON.stringify([{ lastName: "test" }, { lastName: "test" }, { lastName: "test" }])` ? – Abraham Labkovsky Sep 29 '20 at 18:34
  • The param 'fields' has to be an array. From the docs: fields array An array with additional fields – Coen Visser Sep 29 '20 at 18:34
  • And yes, I have tried that but that did not work. I assume they mean an associative array like you've got in PHP, which is why I figured I'd try to use an object – Coen Visser Sep 29 '20 at 18:37
  • Ok, sorry didnt read carefully. What is not coming through? What docs? Where are you sending this data? – Abraham Labkovsky Sep 29 '20 at 18:40
  • No worries, I'm thankful you're even taking the time to help me! I'm trying to use this https://developer.enormail.eu/#contacts-add. It's just the fields that isn't coming through, name, email etc are working fine – Coen Visser Sep 29 '20 at 18:43

0 Answers0