欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Working with APIs

程序员文章站 2022-06-04 13:30:36
...

Accessing the content of the data the server returns

response.content

Importing the JSON library

import json

Getting the content of a response as a Python object

response.json()

Accessing the information on how the server generated the data, and how to decode the data

response.headers

Concepts

  1. An pplication program interface (API) is a set a methods and tools that allow different applications to interact with each other. APIs are hosted on web servers.
  2. Programmers use APIS to retrieve data as it becomes available, which allows the client to quickly and effectively retrieve data the changes frequently.
  3. JavaScript Object Notation (JSON) format is the primary format for sending and receiving data through APIs. JSON encodes data structures like lists and dictionaries as strings to ensure that machines can read them easily.
  4. The JSON library has two main methods:
    • dumps - Takes in a Python object, and converts it to a string.
    • loads - Takes in a JSON string, and converts it to a Python object.
  5. We use the requests library to communicate with the web server and retrieve the data.
  6. An endpoint is a server route for retrieving specific data from an API. Web servers return status codes every time they receive an API request. Status codes that are relevant to GET requests:
    • 200 - Everthing went okay, and the server returned a result.
    • 301 - The server is redirecting you to a different endpoint. This can happen with a company switches domain names or an endpoint’s name has changed.
    • 401 - The server thinks you’re not authenticated. This happens when you don’t supply the right credentials.
    • 400 - The server thinks you made a bad request. This can happen when you don’t send the information the API requires to process your request.
    • 403 - The resource you’re trying to access is forbidden; you don’t have the right permissions to see it.
    • 404 - The server didn’t find the resource you tried to access.

Resources

相关标签: API

上一篇: API

下一篇: quill富文本编辑器 API