在请求后的python中传递json以提交文件

时间:2018-11-26 11:24:07

标签: python json post python-2.x

我正在尝试使用Python在发布请求中传递JSON。 下面是代码

from urllib2 import Request, urlopen        
values = """[{"n": "MyApexClass","r": false,"t": "ApexClass"},{"n": "SysAdmin","r": false,"t": "Profile"}]"""

request = Request('https://copado.herokuapp.com/json/v1/webhook/'
                'commitFiles/user_story_idapi_key=user_api_key&commitMessage=commitMessage', data=values)

response_body = urlopen(request).read()

print response_body

但是我遇到了错误

{"error":"Malformed payload, could not parse metadata json!!!"}

请帮助我。

1 个答案:

答案 0 :(得分:1)

尝试将值不用作字符串,用作python对象

values = [
    {"n": "MyApexClass", "r": False, "t": "ApexClass"},
    {"n": "SysAdmin", "r": False, "t": "Profile"}
]

values = json.dumps(values).encode('utf8')

req = Request(your_url, data=values,
              headers={'content-type': 'application/json'})