HTTP GET PUT POST DELETE

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is part three of asp.net Web API tutorial in this video well discuss HTTP put post and delete verbs this is continuation to part two so please watch part two before proceeding when we talk about a database table row these are the four actions that we can perform on the row create read update or delete a row in the context of an asp.net web api resource these four actions correspond to get post put and delete as shown in the table right here let's now understand some terms and concepts related to HTTP request and response system request verbs these HTTP verbs get post put and delete describe what should be done with the resource for example do you want to create read update or delete an entity get put post and delete HTTP verbs are the most commonly used ones for the complete list of the HTTP verbs please visit this URL right here request header when a client sends a request to the server the request contains a header and a body the request header contains additional information such as what type of response is required for example do you want the response from the server to be in XML JSON or some other format request body request body contains the data you want to send to the server for example a post request contains the data for the new item that you want to create the data format may be in XML or JSON response body the response body contains the data sent as a response from the server for example if the request is for a specific product the response body includes product details either in XML or JSON format response status codes these are the HTTP status codes that give the client details on the status of the request some of the common status quotes are 204 okay for 0 for not found 2:04 no content for the complete list of HTTP status codes and what they mean please visit this URL right here now we'll use a tool called fiddler to perform post put and delete actions you can download fiddler from this URL right here I've already downloaded and installed it now let's modify the values controller that we worked with in our previous video so that it can support post put and delete actions let's now flip to visual studio this is the same example that we worked with in our previous video within our values controller class at the class level let's include a static variable the type is going to be list of string let's name it strings this list is going to contain three strings so it's going to contain value one value two in addition to these two strings let's include another string and the value in this string is going to be value zero and what we want this get method to do is return the list so let's simply return our static variable strings we have another overloaded version of get method which has an ID parameter we want to use this ID as an index for example if we pass zero as the value for ID then we want to return the string that is present at index position zero within our static variable which is value zero if we pass one then return value 1 if we pass two return value two so from this overloaded version of the get method let's return strings of ID post method we use this method to create a new item notice this method has this value parameter which is of type string so whatever string that we are passing to this method we want to add it to our static variable so let's call the add method and pass the value to that put method we use this to update an item notice this method has got two parameters the ID of the item that we want to update the new value with which we want to update it with okay so we are going to use the ID as the index and we want to update the item at that index position with this new value that is also coming in as a parameter we use the delete method to remove an item so let's use this ID to remove an item at the specified index position so strings dot remove add and we pass the ID to that function which is going to remove the string at that index position all right so let's give our solution a build so the build has started as you can see in the status bar it's going to take a few seconds to complete the build has completed now let's reissue the request since we have rebuild the solution it's going to take a few seconds to reload notice we have got three strings as expected let's now launch fiddler and perform post put and delete actions I'm going to click on start all labs scroll down a bit and then select fiddler this should launch fiddler and I'm going to delete this traffic which is already captured let's now issue the get request once again this request should have been captured by fiddler they we have it I'm going to double click on this notice the response that we are getting back is XML notice we have values from 0 till 2 now what I'm going to do is click on the composer tab drag and drop the request on to the composer tab so the requests that we have issued is get this is the URI to which we have issued that request now we want to issue a post request and create a new item so I'm going to click on this drop down notice in addition to get post put and delete we have several other HTTP verbs as well let's select post and the post we also have to specify a request body and I'm going to specify the request body in JSON format so the new item that we want to add to our stead variable is new string and when we send something to the server we have to specify the content type so I'm going to specify that in the header so we use content type for that and the content type is going to be Jason so application for slash Jason and then I'm going to click execute notice the request completed let's double click on that look at the status code that we are getting back to zero for no content it's just a post request we have added a new item to the collection there's nothing to return that's the reason why we get to 0 for no content now let's verify whether if the item has been added to a static variable and to do that I'm going to go to the composer tab once again and then issue a get request with the get request we don't specify a request for T that's the reason why it has become red so I'm going to delete that and then execute this once again that it goes to executed completed successfully now first of all notice you know we have over four strings value 0 1 2 and our new string and if you look at the response header look at that the HTTP status code that we got is 200 ok and what we are getting back here at the moment is XML now if you want the response to be in Jason then you specify that within your request header so let's go to the composer tab and in our get request so here look at that at the moment we accept any of these text slash HTML application slash XHTML or XML now we have XML available you know from the Web API so that's what we are getting but if you want Jason you can specify that using this accept header here so I'm going to delete all this and then specify application for slash Jason and then let's eat reissue the request so request completed successfully let's double click on that notice now what you're getting back is a JSON object we've got value 1 0 1 2 and our new string now we've seen how to perform a post now let's see how to perform our in output action so I'm going to go back to the composer tab and then select port instead of post and let's say we want to update that new string to updated string so we want Jason back and we are going to send Jason to the server so let's specify content type and we want the new string it to be updated string and then and we want to update the last string and the index position for that is 3 so now let's go ahead and execute our request so here is our request again look at that since it's an update we get HTTP status code to 0 for no content let's go ahead and change this to a get request and let's get request we don't have the body so let's go ahead and delete that and let's issue a get request to values URI execute that and here's the request completed successfully and notice that the value is updated to updated string now finally let's issue a delete request let's go back to the composer change this to delete and we want to delete our last item which is pleasant at index position 3 and let's issue the request so it's completed again we get to 0 for no content let's go back to the composer tab and change this to get and execute for some reason we get a 4 C 500 error 500 is internal server error that's basically because if you look at our composer look at the get request that we have issued if we should a get request to an item that is present at index three we don't have an item at index position three that's the reason why we've got that error so let's get rid of that ID there and then when we execute this that a class should complete successfully so the header 200 okay and if we look at the raw data what we are getting back is Jason because that's what we have asked for we want the response format to be in Jason now when we use this index position three we got an exception 500 internal server error we'll discuss how to deal with these exceptions in a later video now if you look at these three methods post put and delete for all these three methods the return type is void and that's the reason why we get the HTTP status code as to zero for no content we have complete control on what status codes we want to return from these methods with asp.net web api will discuss how to do that in a later video thank you for listening and have a great day
Info
Channel: kudvenkat
Views: 603,493
Rating: undefined out of 5
Keywords: asp.net web api post json fiddler, fiddler post request body example, fiddler specify content type, content type json header fiddler, fiddler set content type to json, asp.net web api put method, asp.net web api delete example, fiddler test rest service, fiddler test web api post, web api post json c#, c# web api post json string, c# web api return json, c# web api return xml
Id: GbKBcDX8DDQ
Channel Id: undefined
Length: 12min 13sec (733 seconds)
Published: Tue Aug 30 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.