我正在尝试使用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!!!"}
请帮助我。
答案 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'})