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

2024-10-03 17:21:02 发布

您现在位置:Python中文网/ 问答频道 /正文

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

所以这里有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个状态错误


Tags: importjsonfalsedataapplicationresponsecontentpost
1条回答
网友
1楼 · 发布于 2024-10-03 17:21:02

我试着做类似的事情,下面是我做的。你知道吗

假设restapi用Json对象响应。你知道吗

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

但是,如果响应是Json数组 在数组中循环并将ID附加到数组中

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

此外,我还使用了一个Json对象来更改值。 而不是将其创建为Json字符串。你知道吗

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

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

相关问题 更多 >