From the course: Next.js: Creating and Hosting a Full-Stack Site

Unlock this course with a free trial

Join today to access over 24,500 courses taught by industry experts.

Creating an add-to-cart endpoint

Creating an add-to-cart endpoint

- [Instructor] Cool, so at this point we have an endpoint that will send back the shopping cart for any userId. But the next thing that we're going to need to do is make this a little bit more realistic by adding endpoints for adding and removing items from the user's cart. So, we'll start off by adding an endpoint for adding items to the cart. And what we're going to do here is we're going to use the same path as what we used for the load shopping cart endpoint, but we're going to use a different method, right? So, I mentioned earlier that the GET request is usually used for loading data, but when we want to modify data in some way such as by adding a new item to a shopping cart in this case, we're going to use another method, and in this case, we're going to use a post request to make this happen. So, here's what that's going to look like. We're going to say export async function POST. And this is going to take very similar arguments to the GET request, so we'll just copy that there…

Contents