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