Manipulate Data With the GitHub API!
Now that you have an authentication token, you can use the API to update your GitHub profile! We're going to use the rest of the CRUD operations - create, update, and delete - and their HTTP verb equivalents -
PUT , and
Create a GitHub Repo With POST
You can use the POST HTTP verb whenever you are trying to create something new in your database. Whether it's a new tweet, photo, or post (see what I did there? 😉) - you'll need to create it using POST. Whenever you fill out an online form or use one to sign up for a new account, the default verb associated with this is POST.
How does the API know what you are trying to create? That's where the message body comes in! You can put the data you are trying to create in your request's message body using JSON. Go to Postman, find your GitHub API authentication token, and let's create a new repo using only GitHub's API!
To create a GitHub repository using the GitHub API, we:
Went to GitHub's documentation to see the correct endpoint:
Added our authentication token to our header parameters using Postman.
Added our repository details into our message body in JSON using Postman.
Saw that once we made a successful request with our API, the repo showed up on our GitHub profile UI!
Update a GitHub Repo With PUT/PATCH
You created your GitHub repo but then decided to give it a different name. You can use
PATCH to update an already existing object in your API.
PUT: Updates the entire resource (i.e., replaces everything).
PATCH: Updates only the part of the resource that was sent.
How do you know which one to use for which situation? Check the API documentation!
To update a GitHub repo, we:
Checked the GitHub documentation for the correct URI:
Went to Postman, changed our HTTP verb to PATCH, and entered in the correct owner and repository name for the repository we just created.
Edited the message body to give the repository a new name.
Saw our new repository name in our GitHub UI.
Delete Your GitHub Repo With DELETE
So now you decide you don't really need your GitHub repo - so let's go ahead and DELETE it!
Now you've seen how POST, PUT, and DELETE work and used them yourself with the GitHub API and Postman. You used them in the context of a GitHub repository, but you can use these HTTP verbs on for all different types of functionality depending on the API.
You saw in the GitHub documentation that the correct URI is the same for Update but with a new HTTP verb:
We had to update the name of our repo in the URI to the new name we just edited.
You saw that our repository is no longer there in our GitHub UI!
You can use
POSTto create data.
PATCHlets you update data.
DELETEis easy - you can use it to delete data!
In the next part we'll take a look at best practices when designing an API yourself!