如何在Python中发送POST请求时将项的值传递到数组或列表中?

时间:2015-11-17 12:35:32

标签: python json request

import requests
import json
import urllib2
data = '{"userId":"faraz@wittyparrot.com","password":"73-rRWk_"}'
response = requests.post(url, data=data,  headers=
{"ContentType":"application/json"})
dataa =  json.loads(response.content)
a = dataa['accessToken']
print a
tiketId = a['tokenType'] + a['tokenValue']
print tiketId
wit = '{ "name": "wit along with the attachment","parentId": "6d140705-c178-4410-bac3-b15507a5415e",  "content": "faraz khan wit", "desc": "This is testing of Authorization wit","note": "Hello auto wit"}'
response = requests.post(URLcreatewit, data=wit , headers={"Content-Type":"application/json","Authorization":tiketId} )
createwit =  json.loads(response.content)
print createwit
Id = createwit['id']
WitId = Id
print WitId

所以这里witId是2d81dc7e-fc34-49d4-b4a7-39a8179eaa55作为回应 现在我想将这个witId用于json作为输入:

Sharewit = '{ "contentEntityIds":["'+WitId+'"],"userEmailIds": ["ediscovery111@gmail.com"],"permission":{"canComment": false,"canRead": true,"canEditFolderAndWits": false,"canFurtherShare": false,"canEditWits": false}, "inherit":true}'
response = requests.post(URLcreatewit, data= Sharewit , headers={"Content-Type":"application/json","Authorization":tiketId} )
print response.status_code

所以在最后一个json中,似乎它没有取得witId的值并且给出400状态错误

1 个答案:

答案 0 :(得分:0)

我试图做类似的事情,这就是我做的事情。

假设其余的api以Json对象响应。

id = response.json()["id"]

但是,如果响应是Json数组 通过数组循环并将id附加到数组

item = []
for item in response.json():
    ids.append(item["id")

此外,我使用了Json对象 - 能够更改值。 而不是将其创建为Json String。

sharewit["userEmailIds"] = ["ediscovery111@gmail.com"]
sharewit["contentEntityIds"] = [id]

response = requests.post(URLcreatewit, data=json.dumps(sharewit), headers={"Content-Type":"application/json","Authorization":tiketId} )