Learning REST APIs in 15 Minutes

Structure of a Request

  1. An endpoint
  2. A method
  3. Headers
  4. A body

Endpoints

Methods

  • GET: Used to request data from the server. GET methods should have no additional effect on the system.
  • POST: Used to add new data to the server. Often causing a change in state or side effects.
  • PUT: Used to update data on the server.
  • DELETE: Used to request the deletion of a resource on the server

Headers

"Content-Type: application/json"

Body

Responses

  • 100s: Informational responses
  • 200s: Successful responses
  • 300s: Redirection responses
  • 400s: Client error responses
  • 500s: Server error responses

Using an API

$ pip install requests
response = requests.request(
"GET",
"https://api.github.com/users/octocat"
)
response.json()
{
'login': 'octocat',
'id': 583231,
'node_id': 'MDQ6VXNlcjU4MzIzMQ==',
'avatar_url': 'https://avatars.githubusercontent.com/u/583231?v=4',
'gravatar_id': '',
'url': 'https://api.github.com/users/octocat',
...'followers': 3714,
'following': 9,
'created_at': '2011-01-25T18:44:36Z',
'updated_at': '2021-04-22T14:27:39Z'
}
response = requests.request(
"POST",
"https://api.github.com/user/repos",
json={
"name": "rest-api-tutorial",
"description": "This repository was created as part of a tutorial on REST APIs"
},
headers={"Authorization": f"token {TOKEN}"},
)
print(f"Response: {response.status_code} - {response.reason}")
Response: 201 - Created

--

--

--

Daeploy helps you to create APIs out of your existing #python code and deploy it as web services. Get started now!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Should you attend a Coding Bootcamp?

Why I like the Chromebook

“Peak Framework” and the runtime barrier: why libraries and frameworks are exhausted

Coronavirus disease (COVID-19) and the push to remote working

What the heck is Python?

The Ruby VM and How Apps Break (Part 1)

How to configure Bitrise workflows for your Android project

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
daeploy

daeploy

Daeploy helps you to create APIs out of your existing #python code and deploy it as web services. Get started now!

More from Medium

Receive camera picture from user in Django

Python MongoDB Tutorial: Getting Started with MongoDB in Python

Today Let’s talk about Python environment setup and HTTP REST API using Flask.

Python vs Java — Which is More Compatible for Web Development?