0

I need to send an empty array to Backend API to clear the list of items. After doing a bit of digging it turns out we cannot send an empty array from form data.

Here's the object for your reference:-

{
            "id": "77ca92af-5d1e-4e86-ade6-ed26a4c771ef",
            "user": "user_6f0170ec62384747ae8849661b6d5e1f",
            "name": "Add On Product 1",
            "products": [
                "P1",
                "P2",
                "P3"
            ],
            "price": "88.00",
            "description": null,
            "stock": 250,
            "image": null,
            "status": "ACTIVE",
            "created": "2023-07-17T22:59:02.736050+05:30",
            "updated": "2023-07-18T02:50:39.634638+05:30"
        }

Backend API accepts an empty array of products to clear them. It works fine with json body but not on formdata.

If someone has a workaround to do it with formdata, please let me know...

Error Screenshot:-

Formdata field when [] is used Formdata field when left empty Formdata field when an empty string is used

1 Answer 1

3

FormData cannot represent an empty array since it is a flat data structure for emulating data nesting. However, you can send a JSON string as a field value instead.

The workarounds would be to send it as JSON, or to have some kind of contract with the BE that when you send XYZ it means an empty array.

Form-data:

products: JSON.stringify([])

You might want to give a look to this

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

1 Comment

Makes sense. I guess I'll probably check for null, [], or blank in the backend and clear the list.

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.