GraphQL是比REST更高效、强大和灵活的新一代API标准。Facebook开发了GraphQL并且将其开源,目前其由一大群来自全球各地的公司和个人维护。
GraphQL 使用 Schema 来描述数据,并通过制定和实现 GraphQL 规范 定义了支持 Schema 查询的 DSQL (Domain Specific Query Language,领域特定查询语言)。Schema 帮助将复杂的业务模型数据抽象拆分成细粒度的基础数据结构,而 DSQL 的实现则赋予了前端开发者自由组织和定制请求数据的能力。如果以一张图来表示的话,可以将 GraphQL 看做一条以 通用基础业务数据模型 为基础、将传统后端服务和前端页面紧密且自由地联系在一起的纽带。
graphql一般需要如下三类参数:
query:查询文档,必填
variables:变量,选填
operationName:操作名称,选填,查询文档有多个操作时必填。
标准的 GraphQL POST 请求应当在 HTTP header 中声明 Content-Type: application/json,并且使用 JSON 格式的内容;
POST 报文体中的 JSON 数据中的也需要query、variables和operationName 这三个字段;下面举个实战例子,以快手为例。
1、graphql为字符串形式
import requests
url = "https://live.kuaishou.com/graphql"
payload = "{\"operationName\":\"commentListQuery\",\"variables\":{\"pcursor\":\"174353328393\",\"photoId\":\"3xscaxhp4qwjuuk\",\"page\":1,\"count\":20},\"query\":\"query commentListQuery($photoId: String, $page: Int, $pcursor: String, $count: Int) {\\n shortVideoCommentList(photoId: $photoId, page: $page, pcursor: $pcursor, count: $count) {\\n commentCount\\n realCommentCount\\n pcursor\\n commentList {\\n commentId\\n authorId\\n authorName\\n content\\n headurl\\n timestamp\\n authorEid\\n status\\n subCommentCount\\n subCommentsPcursor\\n likedCount\\n liked\\n subComments {\\n commentId\\n authorId\\n authorName\\n content\\n headurl\\n timestamp\\n authorEid\\n status\\n replyToUserName\\n replyTo\\n replyToEid\\n __typename\\n }\\n __typename\\n }\\n __typename\\n }\\n}\\n\"}"
headers = {
'Origin': 'https://live.kuaishou.com',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
'content-type': 'application/json',
'accept': '*/*',
'Referer': 'https://live.kuaishou.com',
'Cookie': 'did=web_62bf12a9aa82ae952a919547a686f5fa; ',
'Connection': 'keep-alive',
'x-original-url': 'https://live.kuaishou.com'
}
response = requests.request("POST", url, headers=headers, data = payload)
print(response.content.decode())
2、graphql为字典形式
import requests
import json
url = "https://live.kuaishou.com/graphql"
payload_dict = {"operationName":"commentListQuery","variables":{"pcursor":"174353328393","photoId":"3xscaxhp4qwjuuk","page":1,"count":20},"query":"query commentListQuery($photoId: String, $page: Int, $pcursor: String, $count: Int) {\n shortVideoCommentList(photoId: $photoId, page: $page, pcursor: $pcursor, count: $count) {\n commentCount\n realCommentCount\n pcursor\n commentList {\n commentId\n authorId\n authorName\n content\n headurl\n timestamp\n authorEid\n status\n subCommentCount\n subCommentsPcursor\n likedCount\n liked\n subComments {\n commentId\n authorId\n authorName\n content\n headurl\n timestamp\n authorEid\n status\n replyToUserName\n replyTo\n replyToEid\n __typename\n }\n __typename\n }\n __typename\n }\n}\n"}
headers = {
'Origin': 'https://live.kuaishou.com',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
'content-type': 'application/json',
'accept': '*/*',
'Referer': 'https://live.kuaishou.com',
'Cookie': 'did=web_62bf12a9aa82ae952a919547a686f5fa; ',
'Connection': 'keep-alive',
'x-original-url': 'https://live.kuaishou.com'
}
response = requests.request("POST", url, headers=headers, data = json.dumps(payload_dict))
print(response.content.decode())
get请求的三个参数需要直接体现在请求链接里:
?query=查询文档&variables=变量&operationName=操作名称
https://www.cnblogs.com/rongfengliang/p/5966283.html