2017-03-29 49 views
0

我想使用Python的請求,發佈的形式,這種形式使用JSONPython的JSON形式發表DATAS

下面是sendind形式時所發送的JSON DATAS:

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

我的問題是如何使用python和請求發送這些數據?謝謝

回答

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\":\"[email protected]\",\"body\":\"Good photos !!\"}],\"id\":0}' 

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

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