Send null as value in ngResource AngularJS

I am using AngularJS 1.2.1 ngResource and I am trying to send null as a value in parameters in a PUT request. Unfortunately, when it is installed, Angular will ignore its parameter and not send it.

Here is a sample code:

 var User = $resource('/comments/:id', {id:'@id'}, { destroy: { method: 'PUT', params: {content: null} } }); var user = User.destroy({id:123}, function() {}); 

Angular instead ignores content and does not send a key or a null value.

How can I make Angular send null?

+7
javascript angularjs ajax angularjs-resource
source share
2 answers

As Lez noted, the javascript null type is not a valid value for the URL parameter.

The javascript null value does not match the string 'null' . Therefore, if you want your URL to look like /comments/123?content=null , just specify the string 'null' . URL parameters are not JSON, and null in javascript does not mean the same as content=null , because in the latter case, null will be interpreted by any given server as a string value.

Angular $ http service filters out the null and undefined values โ€‹โ€‹in the params object before creating the url with them , because there is no standardized way to send undefined as a "value" to the server. It just doesn't make sense. However, if you want to send a string such as 'undefined' or 'null' , thatโ€™s fine, but interpreting it requires a server-side implementation.

If you make a PUT request, why are some of your data JSON-serialized inside the resource, and some are sent via the URL parameter? Maybe the sample code is simply poorly presented, but in this example you send a PUT request with the response body {"id": 123} and url /comments/123 . Isn't that redundant? Why arenโ€™t you sending the content property and data in the JSON string to the server? Then it will actually be the null value you are looking for, because JSON strings can represent a null value. Also, if you delete / destroy a record, why don't you use the DELETE request, and instead impose a destruction method using the PUT request?

So why not? I'm not sure what User is associated with it ...

 var Comment = $resource('/comments/:id', {id: @id}, { destroy: {method: 'PUT'} }); new Comment({id: 123, content: null}).$destroy(); 
+3
source share

The comments that were made since my question means that null should not be sent as it cannot be encapsulated correctly in the HTTP parameters. However, it can be encapsulated in JSON. Why does anyone want to send a null value? Because sending zero to the endpoint upsert [1] means destruction. Unfortunately, there is no way to set the default value of content to zero in the .destroy() call.

The answer I agreed with was:

 var Comment = $resource('/comments/:id', {id:'@id'}, { destroy: { method: 'PUT' } }); var comment = Comment.destroy({id:123, content: null}); 

This is exactly like @jbielick's answer, and it does an excellent job explaining why it did not work as the default parameter, but works if it was provided as part of the JSONify input object.

  • Upsert is a combination of inserting and updating words.
0
source share

All Articles