# Insert API with HTTP POST - C# on Linux Episode 17

## Метаданные

- **Канал:** IAmTimCorey
- **YouTube:** https://www.youtube.com/watch?v=oAMMHR8kKnw
- **Дата:** 18.05.2026
- **Длительность:** 22:05
- **Просмотры:** 931

## Описание

We are going to add an HTTP POST command to insert data into SQL in our Minimal API project. Plus, we will auto-launch to our Swagger homepage.

Full Training Courses: https://IAmTimCorey.com
Source Code: https://www.iamtimcorey.com/downloads/?code=linux-ep-17

## Содержание

### [0:00](https://www.youtube.com/watch?v=oAMMHR8kKnw) Segment 1 (00:00 - 05:00)

In this episode, let's work on the insert command for API as well as a set up Swagger for automatically launching when the API launches. Now this video is part of a series dedicated developing C sharp applications on Linux. Along the way, we're getting a better knowledge of how Linux works and how to make our apps truly cross-platform. If you have not yet subscribed, I encourage you to do so today that we keep up to date with new episodes as they come out. And if you're looking for even more. NET SQL and API training, go to I'm Tim Corey. com. That's what funds this free content. Okay, let's switch over to Linux and here I have our API service open. Uh quick note, uh during the recording of the series, in fact, or just recently, uh VS Code has updated how I actually the C sharp dev kit has updated how VS Code works. And so now, um instead of using solution explorer, which if you open this now, um doesn't look the same as it used to, we use the actual normal view here instead of the solution explorer. So, the normal view like you would for any other VS Code project, um is how we're going to use from now on with uh the C sharp dev kit. You can still right click on your projects and say, for example, add new package. You can still click on the solution or the I'm sorry, the uh project itself and see all the packages, etc. So, um still all works the same way. They just consolidated back into the normal view instead of having a separate view just for um C sharp projects. Okay. So, in program. cs, this is where we're going to work today. What we're going to do is we're going to add an insert. Now, we already have, let's go over our SQL Server, let's open up our uh ticket DB and go to programmability, stored procedures. We have the insert command. And in here we open up parameters, we'll see we're looking for title description and priority. So, that's what we're going to be looking for is executing that insert command. We already have our um our get and one. Let's just um let's just create a new one uh by hand. So, we're going to say actually before we do, um let's create our model for this. So, notice we have these three parameters, ticket title, ticket description, and priority. Three values. So, what I'm going to do is right down below the run, um this is not what I would do for a large project, but this is not a large project. This is a small project. And one of the things you have to realize when you're building out applications is that you need to make your code work and your design work for the size project you're building. Meaning, you shouldn't have 30 files in this project because this project's way too small for that. And what does that creates confusion difficulty in debugging when you don't need that. So, we're just going to put right down here our records. So, public record uh ticket insert record. And for this record, we're going to have a string ticket title, string ticket description, and uh int priority. Now, if that sounds familiar, that's because it matches up ticket title, ticket description, and priority. Um now, you may be saying why would a start model if we have a model that has those things in it. Sure, it has other properties, but it has those in it. This is one of those things that people get caught up on when they think about dry and they say, "Hey, I don't want to repeat myself. Therefore, I'm not going to create a different model. " Models have different purposes. So, even if they look identical, they're probably not. So, I would encourage you not to get caught up in trying to consolidate down and having less of these. Have the ones you need. So, in this case, an actuation spelled insert with a T. Uh with T. How about that? To get insert record. Um so, this is going to be for inserting records. So, these are three things I need. Okay. So, we have our model. Now, we can come here and create our we can collapse this down here. Um Actually, let's make it open, for example. But, um we're going to say app. map post. So, it's going to be a post command. If you don't really HTTP verbs, I encourage you to read over and at

### [5:00](https://www.youtube.com/watch?v=oAMMHR8kKnw&t=300s) Segment 2 (05:00 - 10:00)

least learn the five basic ones. We'll be using four in this course, but there's get, post, put, patch, and delete. So, we're going to be using the uh get, post, put, and delete. We're not going to use patch. So, the difference in patch and put are both for updating. Um but, put is for basically updating the entire record, whereas patch updates a part of the record. Like, maybe one or two um fields. So, for this, we're going to do a post, which is an insert command. So, uh {slash} API {slash} tickets. And we're going to mark this as async. And we'll say SQL data access. Or we call it Yeah, SQL data access. Not sure why the um IntelliSense didn't find that. That's okay. And then we'll do our arrow function. And then we put a semicolon at the end. Okay. So, there is the basics of Oh, we need one more thing, and that is the ticket insert record. So, ticket insert record, and we'll call this ticket. Okay. So, we're going to call this when I create a new ticket. So, I want to say um await SQL. save data async. So, that's our um our SQL data access, and I'm going to pass into this um my dbo. sp_tickets_insert. And then for the data, I'm passing ticket. And then ticket DB. Okay. So, the data I'm passing in is the parameter is actually this um this insert record. Okay. It's ticket insert record, which since it matches up property for property, I don't have to do anything else. Now, this insert command I feel is done right now, and that will return I believe it returned a 200 saying it's done uh or okay, but I don't really love that because of the fact that um you know, are we expecting data back or we not expecting data back? And this does depend on your um your API. Typically, what I'd probably do is I would probably modify my stored procedure to get back the um either the whole record or at least the ID of the record that was just created and send that back and say, "Hey, here's the ID that was just created and here is the path to look that ID up the {slash} API {slash} tickets {slash} um ID number. That way you could do a get command and get that same record right away. But, I'm not going to do that. I'm just going to say, "Hey, it's inserted. " I'm going to return a no content. But, I want to follow the same pattern we did uh right here where we are saying async um task of and then results will be um I'm getting some weird errors here today. I'm not sure why. Um I might have to restart Visual Studio Code. But, um we're going to say no content and we're also going to say validation problem. Okay? So, that's the um the two things I want to return as a possibilities, which means that down here I can say return typed results {dot} no content. Now, don't worry about the validation problem yet. Um we're going to wait to return that until after we make sure that this works. So, let's um in fact, I'm going to restart Visual Studio Code just in case there is a problem. The cool thing is restarting it takes so little time that um I don't want to see a survey. install a Docker endpoint yet. Um okay. So, with this setup, I want to come down here to run debug. It's probably going to make me um, copy that code, but let's see. Waiting for it to run. Nope, no code needed this time. Okay, so my API service is running. If I click on this, I go to the, um, the page I go to {slash} swagger. We're going to fix this in a minute. And now I have a post command.

### [10:00](https://www.youtube.com/watch?v=oAMMHR8kKnw&t=600s) Segment 3 (10:00 - 15:00)

So, if I say try it out, and let's say, uh, test ticket, and we're going to say test description, and the priority is three. And I hit execute, I get back the, um, 204, which is the no content. So, it completed successfully, but there was no content. Now I can come up here to my, um, {slash} tickets, and try it out, execute, and we can see here, uh, test ticket is right here, date created, priority, etc. Now, I do want you to notice one thing real quick, ID number one, ID number two, ID number three, ID number 1007. Now, I have done worked in this database a little bit from make sure it was working, so I created a couple records and deleted them, um, but it jumped by a thousand. And some people freak out when I adjust this or fix this. There's nothing to fix. In fact, this is a good thing. What happens is SQL Server, when it is shut down unexpectedly, and it doesn't know for sure that it has written all the records. So, what happens with SQL Server, real briefly, is it writes your data to basically a text file, and then it takes that text and puts it into the database. So, there are times when before that file can get put into the database. That, you know, the log file can get put into the actual database. The database crashes. And when that happens, it can't be certain that there's not going to be new records inserted in the database. And so, what it does it gives us, and the default is 1,000, um a blank 1,000 entries so that when a database start back up and that log file is processed, we don't have two IDs of four. Um so, there's a gap in there that can backfill when it processes that log file. So, there's a lot more complexity there, and reasons for it, but that's the basics. So, this is a security or safety thing with SQL Server that's doing to make sure that you don't have duplicate IDs. The ID number does not matter. Ignore it. Stop thinking about it. Stop worrying about it. So, this you don't have to go back and like move things around or recover those IDs. You just don't. Um so, the only thing that matters, really, is that it's sequential, meaning that you don't have um IDs that are all over the place. You have they go in order. That's the only thing that matters for SQL Server is they're stored on disk in the order of their ID. And that is really helpful because of the fact it makes look up that much faster. Um there's a reason why um GUIDs are nice, but they're slower. And the reason why they're slower is because when you store on disk, you store it in order, and so it has to shuffle things around every time you do an insert. Whole another topic, but this just means nothing. Don't worry about the gap here. It just means the server shut down. That's going to happen on dev server, especially, um because I'm shutting my um container down all the time. So, we have an insert. But, let's do another insert. And let's do an insert where um I'm going to insert uh null. empty string. When I set a priority of zero, execute. This fails, right? It just says, "Oh, we got a failure. Um if we keep running this, then we're going to get um a 500 error, which isn't what we want. Um but let's put in here a value. Execute again. This time it worked. 204. So, empty strings work. Priority of zero worked. If we come up here and execute this, we can see now that First of all, 108 was skipped because it failed. Um but then 109, we have empty title, empty description, and priority of zero. That's not a valid priority. title or description. So, we need to fix that. We need to put some validation in. That's why we have that validation option I uh showed you. Okay. So, let's add the validation to this so that we can be sure Let's close this out. Um that we don't put data in

### [15:00](https://www.youtube.com/watch?v=oAMMHR8kKnw&t=900s) Segment 4 (15:00 - 20:00)

we can be sure that we don't put data in that's incorrect. Now, to do this, the first thing in a minimal API is Let's do it after problem details. We say builder. services. addvalidation. Simple enough. Um and why are we Oh. It still thinks that I'm running, which I'm not. Um That's interesting. Let's try this again. Got to love when um a program goes a little haywire. Okay. Should be good. Um So, we have our um our add validation statement in our services. So, that's adding to dependency injection. Now, we can do validation on and let's collapse this one down, too. Uh on our post. But, how do we do that? Well, first command in our record and this is very important. This public you don't have to have in order to use this record with this post command. It will work. But, what will not happen is validation. In order for validation to happen, you have to mark this as public. It has to be accessible outside of your um your class. So, make sure it's marked as a public record. Now, I'm not positive why it says I think it has to do with the fact that if you'd be exposing data from inside the record if like if you have a validation message, I think is the reason. I'm not positive. Um but, what we're going to do is I'm going to put these on their own lines. — [snorts] — Otherwise, it's going to scroll quite a bit. And I'm going to say, let's mark this is required. Okay? And we'll do the same thing for description. Now, I could um also put the um I should just do this. Min length of one. So now, you know what I just put a comma and that puts two different um modifiers on here or attributes on our title and description. So now, both of them have a minimum length of one uh and they're required. And then the priority, I'm going to not say it's required because it is not nullable. Um but I'm going to say a range and this is going to be one to five. Which means the number has to be in that range. Okay. So, records, you can do the same um validation things that you can with classes. Super helpful. All right, let's run this again. Wait for it to be done. — [clears throat] — There we go. So, there was Oh, I haven't changed that over. Let's do that in a minute. But, right now I can go to post. Let's try it out and let's no title, no description, and priority zero. Execute. And it says 400. And it says there's one or more validation errors have occurred. The ticket title field's required, the ticket description field's required, and the ticket or the field priority must be between one and five. So, now even if I put uh test and test here and I execute, we still get a 400, but now it just says the field priority must be between one and five. So, it knows that um this needs to be and we can't [clears throat] say six here because it's still an error. Um but if we say four and execute, then we get a 204. We're good. And if we actually try this out, execute, um we will see down here that notice there's no skipped ones because the validation stopped before even tried. And we now see that the only one we inserted since we put validation on is the 1010 version which has a priority of four and a ticket title description of test. So, that's how you add validation to your uh to your app. So, let's stop this. And what we're going to do is I'm going to do one more modification and that is under properties launch settings. Um we're in HTTPS profile. I'm going to say launch URL and we're going to say {slash} swagger.

### [20:00](https://www.youtube.com/watch?v=oAMMHR8kKnw&t=1200s) Segment 5 (20:00 - 22:00)

I'm saving this. This is what's going to change where we go when we launch our API. So, now if I come back over here and hit run, this will save us some time since we're working on the API. I should have done it earlier, but I keep forgetting. This is what I do in all my projects. Um but notice now the URL says {slash} swagger. So, I click this, it goes right to swagger. Just saved us a whole bunch of time. Okay. So, that's it. That's how we do a post command inside of our API. Um once you have the uh Dapper set up, this is super simple. Stored procedure name, pass in your model. Done. Inserts that data. Same with our get command, you know, you pass in the stored procedure and the value. Done. Like this is all it takes. Um And so now notice we only return no content, but because we have validation on, the validation problem takes care of returning that um that message. And we are really scrolling right now. Um so, let's change that. Um let's put this on a new line. Indent it. And we'll put this on the same line, I think. Uh we can indent it one more, but I Yeah, let's indent it one more. Okay. So, so there we go. So, there is our post command. Again, you can get the source code for this in the description if you'd like a copy of it. Um thanks for watching. And as always, I am Tim Corey. —

---
*Источник: https://ekstraktznaniy.ru/video/51642*