API接收时解析有效负载中的json时出现问题

2024-06-28 20:31:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试了json.dumps所有的方法,但仍然不起作用。我不明白发生了什么事。我使用的是githubv3api,我试图用b64encode更新一个文件,但它拒绝了我,就像拒绝约会一样

我似乎找不到问题:0

import requests
import base64
import json
def byteify(input):
    if isinstance(input, dict):
        return {byteify(key): byteify(value)
                for key, value in input.iteritems()}
    elif isinstance(input, list):
        return [byteify(element) for element in input]
    elif isinstance(input, unicode):
        return input.encode('utf-8')
    else:
        return input
def push_to_github(filename, branch, token):
    url="http://{enterprise}/api/v3/repos/dataWrangling/weightsWebApp/contents/test.json"

    base64content=base64.b64encode(open(filename,"rb").read())

    data = requests.get(url+'?ref='+branch, headers = {"Authorization": "token "+token}, verify='/etc/pki/tls/certs/ca-bundle.crt').json()
    sha = data['sha']

    if base64content.decode('utf-8')+"\n" != data['content']:
        message = {'message':'update','content': byteify(base64content),'sha': sha}
        resp=requests.put(url+'?ref='+branch, headers = {"Authorization": "token "+token}, params = message, verify='/etc/pki/tls/certs/ca-bundle.crt')
        print(message)
        print(data)
        print(sha)
        print(base64content)
        print(resp.text)
        print(token)
        print(resp.url)
    else:
        print("nothing to update")

我从API收到了这个消息

{"message":"Problems parsing JSON","documentation_url":"https://developer.github.com/enterprise/2.17/v3/repos/contents/#update-a-file"}

Tags: importtokenbranchjsonurlmessageinputdata