8

[ Python ] requests 问题 json query 语句 post 传输问题

 2 years ago
source link: https://www.v2ex.com/t/872869
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

问题描述:

遇到一个网页提交的数据是 json 格式,但是里面传入的参数一个是 json query 的查询语句,一个是用户名,使用 Fiddle 可以重发看获取到返回结果,用 python 提交就不行,应该是 json 在传输的过程中没有被正确识别,求大佬指教。

代码表述:

网页端原传入参数为

{"query":"\n    query Profile($usernameOrAddress: String!) {\n  profile(usernameOrAddress: $usernameOrAddress) {\n    id\n    username\n    pfp {\n      ...FullBareAsset\n    }\n    decagon {\n      ...FullBareAsset\n    }\n    banner {\n      ...FullBareAsset\n    }\n    bio\n    twitterUsername\n    discordUsername\n    showListings\n    addresses\n    followedByCount\n    followingCount\n  }\n}\n    \n    fragment FullBareAsset on BareAsset {\n  id\n  provider\n  contract\n  tokenId\n  mediaUrl\n  previewStorageKey\n  previewMimeType\n  previewAspectRatio\n  storageKey\n  mimeType\n  tokenUrl\n  name\n  multimediaUrl\n  aspectRatio\n  metadata\n}\n    ","variables":{"usernameOrAddress":"Datdz172"}}

第一种尝试,直接传入字典-结果是乱码(正确应该是 json 格式的数据)

k = {"query":"\n    query Profile($usernameOrAddress: String!) {\n  profile(usernameOrAddress: $usernameOrAddress) {\n    id\n    username\n    pfp {\n      ...FullBareAsset\n    }\n    decagon {\n      ...FullBareAsset\n    }\n    banner {\n      ...FullBareAsset\n    }\n    bio\n    twitterUsername\n    discordUsername\n    showListings\n    addresses\n    followedByCount\n    followingCount\n  }\n}\n    \n    fragment FullBareAsset on BareAsset {\n  id\n  provider\n  contract\n  tokenId\n  mediaUrl\n  previewStorageKey\n  previewMimeType\n  previewAspectRatio\n  storageKey\n  mimeType\n  tokenUrl\n  name\n  multimediaUrl\n  aspectRatio\n  metadata\n}\n    ","variables":{"usernameOrAddress":"Datdz172"}}
response = requests.post(APIurl, proxies=proxies, json=k, headers=header, verify=False)

第二种尝试,直接传入字典,但是把双引号修改单引号,并把\n 去除-结果是乱码(正确应该是 json 格式的数据)

第三种尝试,对原传入内容进行 json.dumps 格式化,但是把双引号修改单引号,-结果是 POST body missing (正确应该是 json 格式的数据)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK