Consuming HTTP Services in Python Transcripts
Chapter: Consuming RESTful HTTP services
Lecture: Blog explorer: Creating a post
Login or purchase this course to watch this video and the rest of the course contents.
0:02 So we can see the post, that is not really new for us although I do feel like this is a pretty sleek little trick with the dictionary unpacking,
0:09 it's time to add the post, so over here we are going to do a few things, I've already created the publish date text
0:16 because that is just not worth you watching me type that, but we still need to get some additional information, from the user.
0:23 So, we are going to need to get the title, so we could say just input, okay, once we have all of this information we will be able to create
0:32 a dictionary on the Python side here, called post data, exactly like the thing we are working with, so we could say like title=title,
0:41 content=content, we know this is what it's going to expect, view_count=view_count, and published=published_text okay,
0:53 so you might think that we can just send this over directly as it is but it turns out
0:58 we got to do one more little step here, we are going to need the url to submit it too so it will be base url plus, remember our API is if we do a post
1:07 to the general blog collection that will create one and actually even return it
1:11 as the response body, so that is cool and let's go ahead and add some headers, and earlier we said we'd accept application json
1:18 but now we are telling it I am sending you application json right, we are going to turn that into json and send that as the body so on the server side,
1:27 it knows it's not like form encoded data or something to that effect, and now we can send it so we can say the response=request. not get, but post,
1:36 right, there is also a put and there is also a delete. So we'll say post and it takes the url the data, the json and so on,
1:43 so we'll say url, we'll say json=post data, and headers=headers, okay, that is pretty cool, let's go ahead and try sending this to the server,
1:55 I think this actually might work, so we'll say if response, now, you got to be a little careful, what does success mean for a post,
2:02 if you look at the http protocol, it says there is a specific success status code 201 which means created and that is the ideal status code for here
2:14 so we are going to assume that this service is working right, if you get a 200 back that might also mean it worked,
2:20 they just aren't being super careful with their status codes, so let's say if it's not over here we'll say print error creating post,
2:30 and any text that we get back, right, it could be hey like this was not found or it could be access denied you must log in or something to that effect.
2:41 Now, if we do see it let's go ahead and do a print, created this, and I am going to do a print whatever we got back, so we should have a post,
2:50 I am going to actually capture that post, and say response.json, now, if we just sent the data to the server,
2:57 why would it send it back, that's weird, right, well, remember, there is 5 pieces of data in every blog post in our data model,
3:04 there is the title, content, view count, published and id, the id is server generated and maybe they also do other normalization
3:11 like set this to be at least zero, not negative, maybe they do cleanup on this, all sorts of things could have been modified
3:19 and we want what the server believes it created, not what we asked it to create. So this post is going to represent that.
3:25 Alright, you want to see if it works- let's give it a shot. Alright, so let's see what's you first, we have three,
3:32 there is always these three that are static when you get started, I didn't want you to start with an empty set of data,
3:38 then I am going to add so this is our first attempt to create a post, the title is this is the body, I don't want to write the whole blog post,
3:49 and the view count is going to be 10. Here we go, boom, that worked beautifully, now it's important to notice here
3:56 that we set the json, another possibility up here, is we could set the data, if we set the data we have to set a string representation of the json,
4:06 if we set the json itself and not the data, it's going to basically stringify that json, so be careful here.
4:13 Alright, now let's see how this works, let's list and see if our post is actually there, oh awesome, this is our first attempt to create a post,
4:21 let's just add one more because we can. This one has got a lot of views folks, boom, we got it back and then notice right here,
4:31 the id that we got back from the server it ends in 48 a so when I list this is our success story it ends in 48 a,
4:39 that is a direct call to the server, we are not holding onto that data. So that is how we create data using our API, remember, we knew that over here,
4:47 because it said the way you create a new post is you do a post against /api/blog where it doesn't actually say it here, but I am telling you
4:58 the body is the actual post content minus the id. Now we have a much more interesting little blog explorer,
5:05 we can both list the existing post and create new ones for ourselves, up next, we are going to want to update the post,
5:11 what if we made a type error or something and maybe even delete the post like no this is old, this is outdated, I'm done with it, goodbye.
5:18 Much less common, but still, we want to have all the crud operations in our little explorer.