如何在Python中发送POST请求时将项的值传递到字典中?

时间:2015-11-07 19:30:40

标签: python json post request python-requests

我有以下代码:

import requests 
import json
import urllib2

data = '{"userId":"ediscovery111@gmail.com","password":"4-53bAGK"}'
response = requests.post(url, data=data,  headers={"Content-Type":"application/json"})
dataa =  json.loads(response.content)
tiketId = dataa['token_type'] + dataa['access_token']
print tiketId

tiketId是bearer7797d2bf-7cd2-4484-b934-77b9031c119b

现在我想使用以下代码发出POST请求:

data = '{"name":"t", "workspaceId":"0917dbfd-228c-4b07-a7ed-a9812169b878"}'
response = requests.post(createfolder, data=data, headers={"Content-Type":"application/json","Authorization":"tiketId"} )
data1 = json.loads(response.content)
print data1

它给了我以下错误:

  

发布数据时,在SecurityContext中找不到Authentication对象。

我想发送tiketId值(bearer7797d2bf-7cd2-4484-b934-77b9031c119b),但只在字典中传递"ticketId"作为授权值,不起作用。

如何在授权密钥中传递tiketId的值。

1 个答案:

答案 0 :(得分:0)

您当前代码的问题在于您使用字符串while (getline(cin, uline)) { //Check line for tags for (auto it = uline.cbegin(), end = uline.cend(); regex_search(it, end, m, tag); it = m.suffix().first) { ctag = m.str(1); //if closing tag if (ctag[0] == '/') { //Error for closing <text> early if (ctag == "/text" && stack.size() > 1) { cerr << getFromMap(tagMap, "text") << "ERROR on line " << count << " unclosed tags before</text>" << endl; return -1; } //check current tag against back if yes alter stack if (ctag.substr(1) == stack.back() && stack.size() > 0) { stack.pop_back(); } //if not tags are improperly nested throw error else { cerr << getFromMap(tagMap, "text") << "ERROR on line " << count << " improperly nested tags" << endl; } } //if opening tag. else { //Failure to find tag in map if (!(findInMap(tagMap, m[1]))) { cerr << getFromMap(tagMap, "text") << "ERROR on line " << count << " tag not found in map" << endl; return -1; } //Check for bad input if (first) { //Check for <text> being first tag if (m[1] != "text") { cerr << getFromMap(tagMap, "text") << "ERROR on line " << count << " first tag not <text>" << endl; return -1; } //Check for input before <text> if (!m.prefix().str().empty() && !checkEmpty(m.prefix().str())) { cerr << getFromMap(tagMap, "text") << "ERROR on line " << count << " text precedes <text> tag" << endl; return -1; } } //add to stack if all tests passed stack.push_back(m[1]); first = false; } //should output here cout << m.prefix() << getFromMap(tagMap, stack.back()); } cout << endl; count++; } 而不是包含您构建的字符串的变量"tiketId"

所以你应该使用

tiketId

而不是

response = requests.post(createfolder, data=data, headers={"Content-Type":"application/json","Authorization":tiketId} )
评论点之后

编辑问题是变量和字符串之间的混淆