Join me while I learn HTMX and talk at you about it
Twitter: @htmxlabs
So I'm gonna lump together hx put, hx patch, and hx delete, kind of into 1. They're all kind of similar. You could think, you know, h x get is your basic get. H x post is your post. Like get and post are probably what most web developers have used for their entire programming lives.
Lazarus:So put, patch, and delete are the kind of variations on post, I guess, is what I would say. You know, this is not this is just sort of my take on it. And you can use them as they are sort of the original intent. They're, you know, HTTP verbs kind of protocol built into the protocol. They've always been there.
Lazarus:They're just have never really been used. Most most things rec, you know, not recommend but most things just use post because it's ultimately you can accomplish the exact same things. But if you want to use put patch and delete so they do have sort of a different, slightly different ideas. They're all encrypted so you still need to use the cores, the, you know, whatever your CSRF tokens are. You still need to use those, just like you would for a post.
Lazarus:So these are all things that modify records, just like post. And you could think of post as when you are creating something new. That's sort of the original idea. You post to just slash articles, not a specific article. Let's say, you know, you're working with articles.
Lazarus:When you post to that, you're posting a form with all the data contained to build a new article. So put, patch, and delete are slightly different. So PUT also contains your full information for an article but you put that at a specific location like where a specific article is. If there's nothing there, put should create it And then if there is something there, put should update it and make all the changes. So you could think of it as like, insert or update.
Lazarus:And then patch is really if you just want to update some small thing about a specific article. So that also targets, you know, the URL for a patch is, say, you know, articles slash ID, whatever your ID is. And then same with delete, you know, you can't delete nothing. So that's also gonna target a specific URL, articles slash ID. Delete does something a little bit different in HTML only because if you return a blank 200 response, it will delete the target that you put, you know, it'll just remove it from the DOM, which basically is just, you know, kind of an extra little convenience for you.
Lazarus:So all of these are kind of just like the other attributes like hx get and hx post. You can use them. You can mix and match them with hx target to say what you want to happen after you've, you know, completed it. You can update something. You can have a a target there.
Lazarus:You can do an h x confirm, which is if you're gonna be changing stuff in the database, it's a good idea. It'll pop up a little confirmation. We haven't talked about that one yet on the podcast, but that's really useful for just kind of checking with the user to make sure that's what they really want to do. You can use a check swap to sort of decide what happens with the response and where it goes. And all of this again is optional.
Lazarus:It's just sort of conventions. If you want to start following these conventions, which are kind of classic HTML conventions, HTTP conventions, it can help you with clarity, I would say. So if you look and you see, oh, that's an h x put, and say, okay. So that is kind of like a create or an update, and you still have to figure that out yourself on the back end. This doesn't do anything for you.
Lazarus:It's not automatically doing any of this stuff. You're still creating routes for all these, you're giving them, you know, what what type of route it is. Is it a get route? Is it a post? You're still doing that stuff on the back end.
Lazarus:So this is just on the front end. What do you wanna see? Do you wanna see h x post and then, you know, to some URL slash delete? That's one way to do it, is to use a post and go to a new route that's delete. Or you can go to your same articles slash one and do h x delete.
Lazarus:And then you can just look at that and you say, oh, okay. That's gonna be doing a delete. So I think, you know, I I will probably use some of these, but you can always if it's too confusing, if you don't want to sort of get into this, then you could just use post. That's what everybody's been doing for decades. It's fine.
Lazarus:It's good. All of this is not specific to h t m x. These have been around. H t m x just kind of unlocks them and allows you to use it from any element. So again, they are completely optional, but different ways to send in requests for your server to handle.
Lazarus:And if it helps you with clarity, and if it's kind of nice to have in your code a little bit of a description of how this server is supposed to handle this, then I would say use them.