1

I have a JSON array object called contacts:

var contacts = [];

After some processing the value of contacts look like:

[{ "Country":"country 1", "Phone":"123" },{ "Country":"country 2", "Phone":"456" }]

Now I want to add this to the "Contacts" name inside the formdata. For that I use:

var formdata = new FormData();
formdata.append("Contacts", JSON.stringify(contacts));

When I try alert(JSON.stringify(formdata)); on button click I get:

\"Contacts\":\"[{\\\"Country\\\":\\\"country 1\\\",\\\"Phone\\\":\\\"123\\\"},
{\\\"Country\\\":\\\"country 2\\\",\\\"Phone\\\":\\\"456\\\"}]\"}"

The problem is that in the API, its not detecting the list of contacts. I tried using POSTMAN as:

Contacts[0].Country : country 1
Contacts[0].Phone : 123
Contacts[1].Country : country 2
Contacts[1].Phone : 456

API accepts data in that case. API accepts rest of the formdata fields except this, just sharing this info to rule out issue with api.

API accepts a collection of contacts as well as other fields like Name, Age and then Contacts.

7
  • It seems like you're not sending the data as your API expects but you haven't given us any detail about what your API accepts. Also, you've proven that your formdata object has all the info you need, so it's likely not the source of the problem. Commented Aug 13, 2020 at 20:46
  • will add reference now. Thanks :) Commented Aug 13, 2020 at 20:47
  • 2
    Tip: console.log() instead of alert(). Commented Aug 13, 2020 at 20:49
  • @tadman Why? It seems to show the right data? Commented Aug 13, 2020 at 20:49
  • What "API"? You need to tell us more about what it's doing, what it expects, and most importantly, how you're sending this. Commented Aug 13, 2020 at 20:50

1 Answer 1

2

If your API accepts your data in that format, you cannot send it as JSON, you need to create a separate FormData entry for each of the lines you've mentioned.

For example

const contacts = [{ "Country":"country 1", "Phone":"123" },{ "Country":"country 2", "Phone":"456" }];


var formdata = new FormData();

// This could be made fancier but it explains how to fix the problem
for (let i=0; i < contacts.length; i++) {
   formdata.append(`Contacts[${i}].Country`, contacts[i].Country);
   formdata.append(`Contacts[${i}].Phone`, contacts[i].Phone);
}

Note that json-form-data does it for you

const data = {
  Contacts: [{
    "Country": "country 1",
    "Phone": "123"
  }, {
    "Country": "country 2",
    "Phone": "456"
  }]
};

const formData = jsonToFormData(data);
for (const [key,value] of formData.entries()) {
    console.log(`${key}: ${value}`);
}
<script src="https://unpkg.com/json-form-data@^1.7.0/dist/jsonToFormData.min.js"></script>

Sign up to request clarification or add additional context in comments.

3 Comments

@Neo Note that I fixed a bug in my initial implementation
Thanks man. But I am just concerned why a standard practice of code didn't work for me :(
I don't understand the question, it didn't work because your API didn't support JSON for the Contacts data, it requires this form data encoding style. Note that there's a package that does that for you npmjs.com/package/json-form-data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.