Python请求400错误
Python request 400 error
import requests
payload = {"query":"query GamePage_Game($name: String!, $type: DirectoryType!, $limit: Int, $languages: [String!], $cursor: Cursor, $filters: StreamMetadataFilterInput) {\n directory(name: $name, type: $type) {\n id\n displayName\n ... on Community {\n id\n streams(first: $limit, after: $cursor, languages: $languages) {\n edges {\n cursor\n node {\n id\n title\n viewersCount\n previewImageURL(width: 320, height: 180)\n broadcaster {\n id\n login\n displayName\n __typename\n }\n game {\n id\n boxArtURL(width: 285, height: 380)\n name\n __typename\n }\n __typename\n }\n __typename\n }\n pageInfo {\n hasNextPage\n __typename\n }\n __typename\n }\n __typename\n }\n ... on Game {\n id\n product {\n id\n __typename\n }\n streams(first: $limit, after: $cursor, languages: $languages, filters: $filters) {\n edges {\n cursor\n node {\n id\n title\n viewersCount\n previewImageURL(width: 320, height: 180)\n broadcaster {\n id\n login\n displayName\n __typename\n }\n game {\n id\n boxArtURL(width: 285, height: 380)\n name\n __typename\n }\n __typename\n }\n __typename\n }\n pageInfo {\n hasNextPage\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n}\n","variables":{"name":"PLAYERUNKNOWN'S BATTLEGROUNDS","limit":30,"languages":[],"type":"GAME","filters":{"hearthstoneBroadcasterHeroName":"","hearthstoneBroadcasterHeroClass":"","hearthstoneGameMode":"","overwatchBroadcasterCharacter":"","leagueOfLegendsChampionID":"","counterStrikeMap":"","counterStrikeSkill":""}},"operationName":"GamePage_Game"}
r = requests.post('https://gql.twitch.tv/gql', data=payload)
print(r.text)
我遇到的问题是,当我尝试 运行 此脚本从 Twitch 获取信息(观看者计数等)时,我得到的响应是
{"error":"Bad Request","status":400,"message":"invalid request body format"}
。我不明白为什么。
(当我刷新 twitch 页面时,从 Chromes 开发工具的 "Network" 选项卡中获取了负载)
使用json
代替data
:
requests.post('https://gql.twitch.tv/gql', json=payload)
import requests
payload = {"query":"query GamePage_Game($name: String!, $type: DirectoryType!, $limit: Int, $languages: [String!], $cursor: Cursor, $filters: StreamMetadataFilterInput) {\n directory(name: $name, type: $type) {\n id\n displayName\n ... on Community {\n id\n streams(first: $limit, after: $cursor, languages: $languages) {\n edges {\n cursor\n node {\n id\n title\n viewersCount\n previewImageURL(width: 320, height: 180)\n broadcaster {\n id\n login\n displayName\n __typename\n }\n game {\n id\n boxArtURL(width: 285, height: 380)\n name\n __typename\n }\n __typename\n }\n __typename\n }\n pageInfo {\n hasNextPage\n __typename\n }\n __typename\n }\n __typename\n }\n ... on Game {\n id\n product {\n id\n __typename\n }\n streams(first: $limit, after: $cursor, languages: $languages, filters: $filters) {\n edges {\n cursor\n node {\n id\n title\n viewersCount\n previewImageURL(width: 320, height: 180)\n broadcaster {\n id\n login\n displayName\n __typename\n }\n game {\n id\n boxArtURL(width: 285, height: 380)\n name\n __typename\n }\n __typename\n }\n __typename\n }\n pageInfo {\n hasNextPage\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n}\n","variables":{"name":"PLAYERUNKNOWN'S BATTLEGROUNDS","limit":30,"languages":[],"type":"GAME","filters":{"hearthstoneBroadcasterHeroName":"","hearthstoneBroadcasterHeroClass":"","hearthstoneGameMode":"","overwatchBroadcasterCharacter":"","leagueOfLegendsChampionID":"","counterStrikeMap":"","counterStrikeSkill":""}},"operationName":"GamePage_Game"}
r = requests.post('https://gql.twitch.tv/gql', data=payload)
print(r.text)
我遇到的问题是,当我尝试 运行 此脚本从 Twitch 获取信息(观看者计数等)时,我得到的响应是
{"error":"Bad Request","status":400,"message":"invalid request body format"}
。我不明白为什么。
(当我刷新 twitch 页面时,从 Chromes 开发工具的 "Network" 选项卡中获取了负载)
使用json
代替data
:
requests.post('https://gql.twitch.tv/gql', json=payload)