I am trying to post a JSON object to an API with 2 fields. However if these fields are empty (i.e. no values were inputted on the form) I want to send an empty array.
The section of the form allows for 2 ticketing options: paid and free, if free is selected no values will be inputted into these 2 fields.
This is what the pricing
and ticketing
option looks like in my state:
ticketing: "" // this would be either 0 or 1
pricing: [
{
price: "",
currency: "",
}
],
And this is how I send it to my API:
const info = {
ticketing: this.state.ticketing,
price: [
{
currency: this.state.currency,
price: this.state.price,
}
],
}
axios
.post(`https://www.somewhere.com`, {
info
})
When no values are inputted for price
and currency
the form posts:
price: [{}]
0: {}
I would like it to post:
price: []
instead, please let me know how I can do this.
I have updated my answer to show that the API receives the the data in single constant.
Thanks for any help!