News for hackers. Stuff that matters.
Re:Curl is just the hobby by throwaway_4228 on Monday April 22, 2024 @05:37AM (#40112714)
> I would personally perhaps protest against the use of PUT for POSTing JSON, but nobody asked me.

Just wondering, if the resource ID is known, I thought PUT is the recommended method? We usually use POST for creating a new resource for which you don't know an ID yet.

Re:Curl is just the hobby by hkwerf on Monday April 22, 2024 @05:51AM (#40112775)
According to RFC 9110:

- POST: Perform resource-specific processing on the request content.

- PUT: Replace all current representations of the target resource with the request content.

I interpret this as the only immediate side-effect of PUT is supposed to be replacing the target resource with the request content. Everything else is POST, but that does not mean that we can't use POST for everything. Thus, JSON via PUT is not inherently odd at all. Calling a random API using PUT with JSON arguments that executes some code other than replacing a resource, would be odd.

I do think, though, that PUT may very well implicitly create a resource, if the name of that resource is the argument to the put. That is just something that's rather odd, as, often, the server has authority over the names of new resources.

I find PUT particularly helpful, as, given these constraints, I assume that PUT is idempotent; POST is not.

Re:Curl is just the hobby by ffsm8 on Monday April 22, 2024 @06:33AM (#40112997)
You should've added the patch specs too, for completions sake (it's to modify an object, not replace it - leaving unset attributes the same as before the operation)
Re:Curl is just the hobby by sublinear on Monday April 22, 2024 @05:50AM (#40112769)
Auth requests are for a new session ID, not a new user ID.

It's a (C)reate in CRUD, not an (U)pdate.

POST - Create, GET - Retrieve, PUT - Update, DELETE - Delete

Re:Curl is just the hobby by janaagaard on Monday April 22, 2024 @07:26AM (#40113273)
The difference between PUT and POST is weather the command is idempotent or not.

Some APIs us PUT for creating items and let the clients specify the ID. If the IDs are UUIDs, the risk of creating duplicates is so slim that it's neglectable, and if that should happen, it's easy for the client to call again with a different ID. The real advantage of this approach is that if the API call times out, the client can just PUT again without having to fear creating a duplicate record. Recovering from a create with POST that has timed out can be pretty difficult because it can happen that the record was created just fine, but the internet connection died before the OK response reached the client.

Re:Curl is just the hobby by ascar on Monday April 22, 2024 @07:31AM (#40113295)
Aren't you basically describing that you want idempotency?

The RFC 9110 (and also the old 2616) clearly state PUT is idempotent while POST isn't.

9.3.4 PUT

"The fundamental difference between the POST and PUT methods is highlighted by the different intent for the enclosed representation. The target resource in a POST request is intended to handle the enclosed representation according to the resource's own semantics, whereas the enclosed representation in a PUT request is defined as replacing the state of the target resource. Hence, the intent of PUT is idempotent and visible to intermediaries, even though the exact effect is only known by the origin server."

9.2.2 Idempotent Methods

"Idempotent methods are distinguished because the request can be repeated automatically if a communication failure occurs before the client is able to read the server's response. For example, if a client sends a PUT request and the underlying connection is closed before any response is received, then the client can establish a new connection and retry the idempotent request. It knows that repeating the request will have the same intended effect, even if the original request succeeded, though the response might differ."

Re:Curl is just the hobby by plextoria on Monday April 22, 2024 @06:23AM (#40112941)
PUT can be used for Create too, if the user ID is generated by the client
Re:Curl is just the hobby by ascar on Monday April 22, 2024 @07:38AM (#40113338)
According to the RFC's PUT can be used to create a resource as long as the request has a clear target. That doesn't necessarily mean an ID, but just some kind of identifier (composite key, hash of content, url, etc.). When a create instead of update happens the server should respond with a 201 CREATED, which should indicate the location or identifier of the created resource.
Re:Curl is just the hobby by andrewfromx on Monday April 22, 2024 @06:51AM (#40113081)
how can you leave out PATCH?

POST - Create, GET - Retrieve, PUT - Upsert, DELETE - Delete, PATCH - Update

Re:Curl is just the hobby by ascar on Monday April 22, 2024 @07:23AM (#40113255)
It's not part of RFC 9110, so why should it be included?
Re:Curl is just the hobby by robertlagrant on Monday April 22, 2024 @07:58AM (#40113479)
Because this comment chain doesn't mention that RFC. That's in a different one.
Re:Curl is just the hobby by ascar on Monday April 22, 2024 @08:35AM (#40113747)
Fair enough. I lost track of the correct comment chain there.
Re:Curl is just the hobby by rvnx on Monday April 22, 2024 @06:28AM (#40112970)
The protocol could be:

CREATE /xxx

READ /zzz

UPDATE /yyy

DELETE /abcd

Re:Curl is just the hobby by maple3142 on Monday April 22, 2024 @07:42AM (#40113361)
Isn't it already supported by REST? CREATE: POST READ: GET (or QUERY if body is needed) UPDATE: PUT for full update, PATCH for partial update DELETE: DELETE
Re:Curl is just the hobby by dgan on Monday April 22, 2024 @06:48AM (#40113069)
Should have SET, because CREATE implies error if anything already exists, but SET would override it
Re:Curl is just the hobby by bayindirh on Monday April 22, 2024 @06:44AM (#40113055)
Why not create an RFC and start the process? :)
Re:Curl is just the hobby by omnibrain on Monday April 22, 2024 @05:49AM (#40112766)
I also wondered, did some research and look at some examples and came to the opposite conclusion for my API.
Re:Curl is just the hobby by thaumasiotes on Monday April 22, 2024 @05:56AM (#40112798)
> Just wondering, if the resource ID is known, I thought PUT is the recommended method?

Recommended for what purpose?

The set of recommendations I'm familiar with is:

    GET:   requests without side effects
    POST:  requests with side effects
    PUT:   never use
    other: never use
POST gets special treatment from browsers for various security risks. Otherwise, methods don't differ. You can use PUT as part of an effort to feel like you and your server have a secret code, but it's fundamentally the same thing as doing `GET /path/to/resource/put`.
Re:Curl is just the hobby by The_Colonel on Monday April 22, 2024 @07:03AM (#40113151)
PUT has a big advantage in that it is mandated to be idempotent - it is safe to be repeated if it fails for whatever reason.

Meanwhile, POST is generally not considered safe to repeat in case of failure, because the client/proxy does not know where exactly the failure happened and if thus the processing has been already done or not.

Re:Curl is just the hobby by thaumasiotes on Monday April 22, 2024 @10:00AM (#40114466)
Yes, that's why you use POST for everything with effects.

But if this is really your concern, you should also be using PUT to create new database records. In principle, those requests are safer to repeat than updates are, since the database will ignore repeated inserts and apply repeated updates. (Though as far as I can tell this only matters if your update adjusts a value rather than setting it outright.)

Re:Curl is just the hobby by The_Colonel on Monday April 22, 2024 @10:43AM (#40114862)
> Yes, that's why you use POST for everything with effects.

I don't get it. Isn't an idempotent effect still an effect?

> you should also be using PUT to create new database records.

If the client is free to define the identity, then I do. But if the resource gets e.g. an ID from Postgres sequence, then I need to use POST, because repeated call would create duplicates.

Re:Curl is just the hobby by foota on Monday April 22, 2024 @06:01AM (#40112828)
I don't think this is quite accurate, browsers won't treat PUT requests as safe the same way it does a get request.
Re:Curl is just the hobby by eptcyka on Monday April 22, 2024 @06:37AM (#40113011)
PUT's can have request bodies, GETs can not.
Re:Curl is just the hobby by thaumasiotes on Monday April 22, 2024 @06:53AM (#40113093)
Incorrect; GET requests are free to have bodies.

See the note here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GE...

Re:Curl is just the hobby by The_Colonel on Monday April 22, 2024 @07:09AM (#40113185)
It's still a rather bad idea. An older version of HTTP spec said that GET request body SHOULD be ignored. Proxies implementing that older version of spec could just drop the body ... You might also encounter problems with many tools not supporting it simply because the authors were not aware of it or perhaps considered it useless.
Re:Curl is just the hobby by eptcyka on Monday April 22, 2024 @07:26AM (#40113275)
Also see the first row in the table on that page.
Re:Curl is just the hobby by yencabulator on Monday April 22, 2024 @11:39AM (#40115418)
If you want a GET-with-body that actually works, use QUERY. The draft is expired, but at least it's just a non-standard method (which is fine!), not actively broken like GET-with-body.

https://datatracker.ietf.org/doc/draft-ietf-httpbis-safe-met...

Re:Curl is just the hobby by cco on Tuesday April 23, 2024 @03:53PM (#40136491)
Loving the nerd sniping here haha.

Join us at Stytch (https://stytch.com/careers) if you have a strong opinion so you can decide whether this should be a POST or PUT :)