I'm trying to send a HTTP PUT request to an API that accepts a JSON object in its body. Everything works fine, except if I want to set a prop of the body to undefined
, that prop will be stripped out entirely from the response object (as if I would call the delete
operator for that prop). I checked and I can confirm that the given prop is there, with the value of undefined
as long as the object is passed to httpClient.put()
.
Does anybody know why does this happen? Is this an expected behavior for the http client, or there's a bug somewhere?
The behavior you describe it's the expected behavior of HttpClient
. Actually, it's the behavior of JSON.stringify()
which HttpClient
uses behind the scenes.
Unlike JavaScript, JSON doesn't have the concept of undefined
. If something is undefined, it's just not defined. It doesn't exist at all.
So, when JSON.stringify()
gets an object with a property that equals undefined
, it omits him.
If you need this property in your request's body, try to change its value to null
, empty string, or zero (depending on your server-side).