Python Requests库使用2:请求方法

版权声明:本文为 [onefine] 原创文章,转载请注明出处: https://blog.csdn.net/jiduochou963/article/details/87871239

GitHub API

URL: https://developer.github.com

HTTP verbs1

Where possible, API v3 strives to use appropriate HTTP verbs for each action.

Verb Description
HEAD Can be issued against any resource to get just the HTTP header info. 仅获取HTTP响应头信息。
GET Used for retrieving resources. 检索资源
POST Used for creating resources2. 创建资源
PATCH Used for updating resources with partial JSON data. For instance, an Issue resource has title and body attributes. A PATCH request may accept one or more of the attributes to update the resource. PATCH is a relatively new and uncommon HTTP verb, so resource endpoints also accept POST requests. 用于使用部分JSON数据更新资源。
PUT Used for replacing resources or collections. For PUT requests with no body attribute, be sure to set the Content-Length header to zero. 替换资源或集合。
DELETE Used for deleting resources. 删除资源

另外:

  • OPTIONS: 查看可用请求方法

使用

requests.[method](url)
request method——GET

https://developer.github.com/v3/users/ 的用法:

import json
import requests

URL = 'https://api.github.com'

def build_uri(endpoint):
    return '/'.join([URL, endpoint])

def better_print(json_str):
    return json.dumps(json.loads(json_str), indent=4)

def request_method():
    response = requests.get(build_uri('users/onefine'))
    print(better_print(response.text))

if __name__ == '__main__':
    request_method()

输出:

{
    "login": "onefine",
    "id": 15047276,
    "node_id": "MDQ6VXNlcjE1MDQ3Mjc2",
    "avatar_url": "https://avatars1.githubusercontent.com/u/15047276?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/onefine",
    "html_url": "https://github.com/onefine",
    "followers_url": "https://api.github.com/users/onefine/followers",
    "following_url": "https://api.github.com/users/onefine/following{/other_user}",
    "gists_url": "https://api.github.com/users/onefine/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/onefine/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/onefine/subscriptions",
    "organizations_url": "https://api.github.com/users/onefine/orgs",
    "repos_url": "https://api.github.com/users/onefine/repos",
    "events_url": "https://api.github.com/users/onefine/events{/privacy}",
    "received_events_url": "https://api.github.com/users/onefine/received_events",
    "type": "User",
    "site_admin": false,
    "name": "onefine",
    "company": null,
    "blog": "",
    "location": null,
    "email": null,
    "hireable": null,
    "bio": null,
    "public_repos": 5,
    "public_gists": 0,
    "followers": 0,
    "following": 1,
    "created_at": "2015-10-09T09:10:16Z",
    "updated_at": "2019-02-17T07:31:36Z"
}

更改:

def request_method():
    response = requests.get(build_uri('user/emails'), auth=('onefine', '******'))
    print(better_print(response.text))

结果:

[
    {
        "email": "[email protected]",
        "primary": true,
        "verified": true,
        "visibility": "public"
    }
]

参考:
python的扩展包requests的高级用法: https://www.cnblogs.com/guhao123/p/4054177.html


  1. https://developer.github.com/v3/#http-verbs ↩︎

  2. 有时候用于修改资源,当这并不符合RESTful规范,更好的方法是PATCH↩︎

猜你喜欢

转载自blog.csdn.net/jiduochou963/article/details/87871239