Python 3.x 作为POST请求发送列表
我正试图用python调试这段PHP代码(这段代码在许多其他代码中,可能会把问题弄得一团糟)Python 3.x 作为POST请求发送列表,python-3.x,python-requests,Python 3.x,Python Requests,我正试图用python调试这段PHP代码(这段代码在许多其他代码中,可能会把问题弄得一团糟) $sql=rtrim(“插入到someTable(foo、bar、id)值中”).str_repeat((?,?,$id),”,count($contents)),,,”; $stmt=$dbh->prepare($sql); foreach($i=>$line形式的内容){ $stmt->bindParam(2*$i+1,$line['foo'],PDO::PARAM_STR); $stmt->bin
$sql=rtrim(“插入到someTable(foo、bar、id)值中”).str_repeat((?,?,$id),”,count($contents)),,,”;
$stmt=$dbh->prepare($sql);
foreach($i=>$line形式的内容){
$stmt->bindParam(2*$i+1,$line['foo'],PDO::PARAM_STR);
$stmt->bindParam(2*$i+2,$line['bar',PDO::PARAM_STR);
echo$line['dutch'].->.$line['english'.\n';
如果(!$stmt->execute()){
$dbh->rollBack();
返回false;
}
}
$dbh->commit();
返回true;
为了进行调试,我编写了这段代码
导入请求
s=请求。会话()
内容=[
{'foo':'f','bar':'b'},
{'foo':'o','bar':'a'},
{'foo':'o','bar':'r'}
]
s、 职位(”http://localhost:8000,数据={'content':content})
无法将数据添加到我的数据库
所以,我尝试了一些调试
print\r($\u POST)
返回的
Array
(
[someOtherVariables] => otherValues
[content] => foo
)
我原本希望内容包含一个对象数组,但不知何故它只包含第一个索引的名称。
有人知道为什么这个python代码没有发送完整的列表,以及我如何修改我的代码使其工作吗
我还尝试使用json而不是数据变量
s.post(“http://localhost:8000“,json={'content':content})
但这似乎并不能解决我的问题。我认为可能是在通过请求发送之前,您需要将python dict转换为json字符串
import json
# a Python object (dict):
x = {
"name": "John",
"age": 30,
"city": "New York"
}
# convert into JSON:
y = json.dumps(x)
就你而言:
contentAsjsonString = json.dumps(content)
s.post("http://localhost:8000", data={'content': contentAsjsonString })