Transcript for:
Using cURL with REST APIs

all right i'm going to give you an example of how to interact with the rest api via the curl command curl is a simple command line program for making http requests it's probably the most minimal way of doing that it's really nice because it's available everywhere it's free you can use it on linux mac probably windows definitely through the wsl layer and ubuntu so it it's a very nice sort of bare-bones way of making uh interacting with the rest api um it it'll also you can incorporate it and say like bash scripts or something else like that um it's very nice it so i'm just going to dive into this i'm going to continue with my example of my to-do list this is also just kind of an example of how to interpret the sort of convention for describing a rest api through documentation so for this first line we have it starts off with the http method so get the domain is omitted usually it's just kind of known or implied it's pretty obvious so so if you're like working with the twitter api for example you know what the domain is going to be obviously so for my to-do list it's going to start off with this static string api followed by a sort of a user it's really sort of a user's to-do list resource so um the the list only takes two operations get is going to um return all the items the user has so their full to-do list post is going to append to it so again this colon means what follows is basically a variable you're going to replace with a particular user's id or in this case i just use their name also i'm going to note here that if as a parameter you're supposed to send a json object with a field called text and it's going to be whatever text you want um so there's the next part of my list or rather my my application here is there's a particular to do item so it supports get put and delete um this get means will return a particular to-do item so you know joe blows item you know with id5 or something like that put is going to update the uh to do item the only thing we're going to update it with is whether the item's completed or not so it has that field as well by default they're all going to be uncompleted to start with and they'll be able to mark them as completed and then finally the last operation we can do to a to-do list item is delete it so let me run you through what i have here with curl i've written out maybe the downside of curls it's sort of lengthy if you write it all out particularly if you're using json so you're going to start with curl command literally curl the first option h is a way of specifying any header field you want in that case because i'm sending in json i need to specify a header field saying the content type so what the body of the request is sending so remember http request there's a header which has a bunch of metadata and a body which is typically just the data being sent this header field says oh by the way interpret the body as json the next piece x selects the http method so i'm going to select post that means i'm running this command to create a new item for adam um oh i jumped ahead of myself slightly so adam's the particular user i'm running this server locally so you can see the full url here you can see how this corresponds this bit up here i've just replaced colon user with a particular user's name adam now dash d says let's specify the data or really the body of the request that's this string here which is json so it's it's an object you can tell by the curly brace has one field called text that corresponds to this piece up here and i'm going to say my thing to do is to start a to-do list that's kind of meta i couldn't think of anything better but here we go so i fired it off it returns to me an empty json object that's just how i chose to implement this sometimes you don't even get that back you might just get nothing unless the status is set i chose return this now i can run the command so let me run um again to see all the to-do list items uh that adam has so let me pop this up i'll get rid of this and i'll change this to get i could technically drop the header too because i'm not sending any data in so but i'm just going to leave that there because i don't want to make a typo and have the whole thing break so i'm going to make a get request to this url now we're going to receive json back so i'm in this case i'm it returns to me a list it has a single item in it an object has an id of 14 uh create a date with a time stamp of when i create the item that text i sent in and this completed field would just mark the false oh that's cool um so let's make another item you can call uh think of a different example hey that's good i'm just going to use examples from now on there me coming up with like meta examples examples of trying to come up with examples i think that's a pretty good uh generator of new ideas so i'm going to run this again so i run the get request again wow look at this now i have yet another one great so now i'm going to run an update so let's say i thought of a different example because i just thought of my uh my example meta so i'm going to change this now i'm going to be using this this entry here right so i'm changing the header type to put the match that means oh put remember get corresponds to read post corresponds to create put corresponds to delete or the update excuse me and delete corresponds to delete i was looking ahead of myself i can't i have to be totally focused anyways um so i don't need uh or let me see i want to update number 15 so i'm going to add that into the url because that's what this endpoint specifies under api you pick a user you give the id of the item to update it is 15. um the other thing is i have to send in a field with completed and i can set it to either true or false up so i'll get rid of this specify true run it okay send me back that now i'm going to just get everything hey what do you know completed is set to true it was false before what do you know so i made the put request sent in how i wanted the field to be specified it could only be true or false and it got updated that's fantastic let me also show um hold on so one useful command is clear i'm going to cat so i'll display the end point again and then okay these are the fields that are there now i will um what's the next oh i want to show you get so i can get just item 15 so that's what this would be so i'm going to say read at you know a user's particular to do use that to do item resource so i'm going to just put 15 on the end run that sweet it turns returns just that particular object it's not a list just that object all the data is there finally i'm just going to finish this example by doing this delete so all i need to do is change the header field here to delete i can run it then i'm going to use this endpoint up here just to get everything adam has that's unfortunate if that's so that's actually good i did that um that sometimes if if you're making a rest api request and some you're getting back weird errors or something like that saying oh you can't find it sometimes having this slash or not having that slash will make a difference depending upon how it's implemented i'm using flask as my back end i think it's kind of picky about having not there so i took off that slash now it works i can see everything adam has it's deleted so that's using curl to interact with the rest api