Python JSON表单发布数据

时间:2017-03-29 10:41:18

标签: python json forms python-requests

我想使用Python发布一个表单请求,该表单使用JSON

以下是sendind表单时发送的JSON数据:

{"method":"PostMessage",
"params":["6drsebmMzn0xYSogAfd_X4DojKa7OpR9QDYAH41lm80xMYjL225zOeeYow5it1l6TCsiOrGb6cvbU50hN11EzkJF9gtlZ3Wgk3ipOO1zOKdcrFFKIudz-ct95EogJSNu",{"ownerId":256887569,"id":18642094,"type":2},{"posterName":"Lionel",
"isPrivate":false,
"posterUrl":"",
"posterEmail":"monemail@gmail.com",
"body":"Good photos !!"}],
"id":0}

我的问题是如何使用python和请求发送这些数据?谢谢

1 个答案:

答案 0 :(得分:0)

最简单的方法是:

import requests

# generate data as string (you may want to automate this line)
data = '{\"method\":\"PostMessage\",\"params\":[\"6drsebmMzn0xYSogAfd_X4DojKa7OpR9QDYAH41lm80xMYjL225zOeeYow5it1l6TCsiOrGb6cvbU50hN11EzkJF9gtlZ3Wgk3ipOO1zOKdcrFFKIudz-ct95EogJSNu\",{\"ownerId\":256887569,\"id\":18642094,\"type\":2},{\"posterName\":\"Lionel\",\"isPrivate\":false,\"posterUrl\":\"\",\"posterEmail\":\"monemail@gmail.com\",\"body\":\"Good photos !!\"}],\"id\":0}'

# post
r = requests.post(url, data=data)

# print result
print r.text.encode('utf-8')