Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#18465 closed defect (invalid)

JsonRest add put issue

Reported by: coke.lightware Owned by: coke.lightware
Priority: undecided Milestone: tbd
Component: Data Version: 1.10.0
Keywords: Cc:
Blocked By: Blocking:

Description

When you define a jstonrest store with an idproperty, and later you try to do an add to including a value in the ID field, it will allways try to do a put.

return xhr(hasId && !options.incremental ? "PUT" : "POST", {
				url: hasId ? this._getTarget(id): this.target,
				postData: JSON.stringify(object),
				handleAs: "json",
				headers: lang.mixin({
					"Content-Type": "application/json",
					Accept: this.accepts,
					"If-Match": options.overwrite === true ? "*" : null,
					"If-None-Match": options.overwrite === false ? "*" : null
				}, this.headers, options.headers)
			});

it never checks for the option overwrite and it will tray to do the the put if you have specified and id on the object.

return xhr(hasId && !options.incremental && options.overwrite ? "PUT" : "POST", {
				url: hasId && !options.incremental && options.overwrite ? this._getTarget(id): this.target,
				postData: JSON.stringify(object),
				handleAs: "json",
				headers: lang.mixin({
					"Content-Type": "application/json",
					Accept: this.accepts,
					"If-Match": options.overwrite === true ? "*" : null,
					"If-None-Match": options.overwrite === false ? "*" : null
				}, this.headers, options.headers)
			});

I've modified the source to check the value of option.overwrite when deciding if its a put or post method and to decide the url.

but i need to specify the overwrite option when calling the put method.

Change History (4)

comment:1 Changed 5 years ago by ben hockey

Owner: changed from Kris Zyp to coke.lightware
Status: newpending

i'm not sure what you're expecting. it looks like you want to do POST target even when you provide the id. here's how it should be working:

PUT target/id - means to replace (or add) the resource using the id and content you've provided POST target/id - means to only update the fields you've provided (this is an incremental update) POST target - means add a new resource and let the server select the id.

the PUT is the correct thing to do when you provide the id even if you want to add a new resource rather than replace an existing one. to the server it means the same thing: "whether or not this thing already exists, put this content at the id indicated by the URL." if you want the server to generate the id then just don't include one in the resource you're creating.

is there something i don't seem to be understanding about your situation?

comment:2 Changed 5 years ago by coke.lightware

Status: pendingnew

Example:

I have a table with Username and email were the username is the ID of the table, so when i want to do a POST to create a new user I send username and email, since the id is inside the object sended the method will try to do a PUT (target/username) but it will return a not found error.

I guessed that if you call the method add of the store it should make a POST (target) whatever information is in the object send. Also the original code never checks for the option Overwrite, that checks if the object should be added or updated, and option.incremental it appears always as undefined in all the tests a made.

comment:3 in reply to:  2 ; Changed 5 years ago by ben hockey

Resolution: invalid
Status: newclosed

Replying to coke.lightware:

Example:

I have a table with Username and email were the username is the ID of the table, so when i want to do a POST to create a new user I send username and email, since the id is inside the object sended the method will try to do a PUT (target/username) but it will return a not found error.

this is a shortcoming of the server rather than a problem with the client code. the server is not properly responding to PUT target/id. it sounds like it's trying to do an update but can't find the thing to update so it returns a not found error. rather it should be ignoring what might exist and just do what you told it to do - "put these contents at target/id and ignore what was already there" - this way PUT target/id works both as update and as add.

this is how the behavior is specified in http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6

The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI.

your server is missing the 2nd half of that.

comment:4 in reply to:  3 Changed 5 years ago by coke.lightware

AAAAMMM Thanks for the clearing that up, all have to talk to my server side programer colleague so that he fixes that. and ill return my source code to normal. Replying to neonstalwart:

Replying to coke.lightware:

Example:

I have a table with Username and email were the username is the ID of the table, so when i want to do a POST to create a new user I send username and email, since the id is inside the object sended the method will try to do a PUT (target/username) but it will return a not found error.

this is a shortcoming of the server rather than a problem with the client code. the server is not properly responding to PUT target/id. it sounds like it's trying to do an update but can't find the thing to update so it returns a not found error. rather it should be ignoring what might exist and just do what you told it to do - "put these contents at target/id and ignore what was already there" - this way PUT target/id works both as update and as add.

this is how the behavior is specified in http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6

The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI.

your server is missing the 2nd half of that.

Note: See TracTickets for help on using tickets.