Reputation: 298
With python I am trying to debug this PHP code (which is inside a lot of other code that would clutter the question)
$sql = rtrim("INSERT INTO someTable (foo, bar, id) VALUES " . str_repeat("(?, ?, $id)," , count($contents)), ',');
$stmt = $dbh->prepare($sql);
foreach ($contents as $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';
if (!$stmt->execute()) {
$dbh->rollBack();
return false;
}
}
$dbh->commit();
return true;
for this debugging I made this piece of code
import requests
s = requests.Session()
content = [
{'foo': 'f', 'bar': 'b'},
{'foo': 'o', 'bar': 'a'},
{'foo': 'o', 'bar': 'r'}
]
s.post("http://localhost:8000", data={'content': content})
Which failed to add the data to my database
So, I tried a little debugging
print_r($_POST)
which returned
Array
(
[someOtherVariables] => otherValues
[content] => foo
)
I had expected content to contain an array of objects yet it somehow contains only the name of the first index. Does anyone know why this python code is not sending the full list and how I could modify my code so that it does work?
I have also tried sending the data with the json instead of the data variable s.post("http://localhost:8000", json={'content': content})
but that does not seem to solve my problem.
Upvotes: 0
Views: 53
Reputation: 1487
I think it could be that you need to convert your python dict to a json string, before sending it via requests.
import json
# a Python object (dict):
x = {
"name": "John",
"age": 30,
"city": "New York"
}
# convert into JSON:
y = json.dumps(x)
In your case:
contentAsjsonString = json.dumps(content)
s.post("http://localhost:8000", data={'content': contentAsjsonString })
Upvotes: 1