Option 1
instead of setting the key to be possibly null, you should set the value as possibly null. if your object has {"expiration": null}
in it, then the field should be ignored, so you could try using:
key: expiration
value:
{{ add_document_expiration_date.value != null? add_document_expiration_date.value : null}}
or just
{{ add_document_expiration_date.value }}
because if it's null... then good, we don't want to change anything that way we end up with {"expiration": null}
Option 2
Just change the body type from Form Data
to Raw
and build your JSON object manually.
when you use Form Data
the Content-Type
header is automatically set to multipart/form-data
. Likewise, when you use Raw
it actually sets the header to multipart/form-data
as well, so the only difference (that we care about here) is that Form Data
gives you a GUI to use and Raw
lets you manually build the JSON object that gets sent.
{
'file': "data:" + {{ add_document_type.value }} + ";base64," + my_base64_string,
"document_type": {{ add_document_type.value }},
"note": {{ add_document_note.value }},
{{ ...(add_document_expiration_date.value != null && {"expiration": add_document_expiration_date.value} }}
}
// the Logical AND (&&)
// - when expression is `False`
// -returns the LHS (Left Hand Side.... whatever is to the left of &&)
// - add_document_expiration_date.value
// - when expression is `True`
// -returns the RHS(Right Hand Side.... whatever is to the right of &&)
// - {"expiration": add_document_expiration_date.value}
// next we use the Spread Operator (...) to copy the key/value pair for either the LHS or RHS that gets returned from above
// - when copying RHS, we copy all the keys and values from `{"expiration": add_document_expiration.value}` into our object after the `"note"` property
// - when copying LHS, the value is null and never an object, the spread operator won't do anything in this case so nothing is copied into our object
EDIT: some people might now be confused on the difference between these and JSON
body type. with JSON
you also get a GUI like Form Data
only every field can only be of types text
, number
or object
where as in Form Data
you can specify lots more like file
. The other difference is the Content-Type
, with JSON
you get application/json
instead of multipart/form-data
.... so the body contents are the exact same as Raw
, but you're telling the server you're sending it application/json
instead and when the server checks the Content-Type
, which it does before looking at the body, and if it isn't supported then you, usually, get a 415 Error (instead of the server checking the body content then trying to figure out what it's suppose to be.... that isn't its job, its our job to tell the server what we send it so that we know it isn't misinterpreted). And Bob's your uncle
.