python - 在 Myjson.com API 上使用 Python Requests.put 时出现 500 服务器错误

标签 python json api python-requests

这里是Python新手。我正在使用http://myjson.com/api托管 json 文件存储,但在使用 Python 使用其 API 进行 PUT 时遇到问题。

我有一个名为 budgetdict,我想将其放入特定的 API 端点。它说“以字符串形式发送更新的 JSON”。

这是我输入 budget 时的响应,我认为这是一个 dict?:

{'spend': [{'isTransfer': False, 'catTypeFilter': 'Personal', 'rbal': 0.0, 'st'
 1, 'isIncome': False, 'ex': False, 'isExpense': True, 'cat': 'Auto Insurance',
'amt': 150.0, 'date': '02/01/2016', 'aamt': 0.0, 'bgt': 150.0, 'type': 1, 'tbgt
: 1800.0, 'islast': False, 'id': 65369859, 'ramt': 0, 'period': 12, 'pid': 14},
{'isTransfer': False, 'rbal': -929.44, 'st': 3, 'ex': False, 'isExpense': True,
'cat': 'Auto Payment', 'amt': 1816.44, 'bgt': 887.0, 'type': 0, 'isIncome': Fal
e, 'catTypeFilter': 'Personal', 'id': 65369856, 'ramt': 0, 'pid': 14}, {'isTran
fer': False, 'rbal': 183.31, 'st': 1, 'ex': False, 'isExpense': True, 'cat': 'G
s & Fuel', 'amt': 266.69, 'bgt': 450.0, 'type': 0, 'isIncome': False, 'catTypeF
lter': 'Personal', 'id': 65369851, 'ramt': 0, 'pid': 14}, {'roll': True, 'isTra
sfer': False, 'rbal': -188.32, 'st': 3, 'isIncome': False, 'ex': False, 'isExpe
se': True, 'cat': 'Service & Parts', 'amt': 238.32, 'bgt': 50.0, 'type': 0, 'ca
TypeFilter': 'Personal', 'id': 65369855, 'ramt': 238.32, 'pid': 14}, {'isTransf
r': False, 'rbal': -14.99, 'st': 3, 'ex': False, 'isExpense': True, 'cat': 'Int
rnet', 'amt': 49.99, 'bgt': 35.0, 'type': 0, 'isIncome': False, 'catTypeFilter'
 'Personal', 'id': 65369845, 'ramt': 0, 'pid': 13}, {'isTransfer': False, 'rbal
: -88.04, 'st': 3, 'ex': False, 'isExpense': True, 'cat': 'Mobile Phone', 'amt'
 245.04, 'bgt': 157.0, 'type': 0, 'isIncome': False, 'catTypeFilter': 'Personal
, 'id': 65369858, 'ramt': 0, 'pid': 13}, {'isTransfer': False, 'rbal': 80.63, '
t': 1, 'ex': False, 'isExpense': True, 'cat': 'Utilities', 'amt': 74.37, 'bgt':
155.0, 'type': 0, 'isIncome': False, 'catTypeFilter': 'Personal', 'id': 6536985
, 'ramt': 0, 'pid': 13}, {'isTransfer': False, 'rbal': 74.0, 'st': 1, 'ex': Fal
e, 'isExpense': True, 'cat': 'Life Insurance', 'amt': 0, 'bgt': 74.0, 'type': 0
 'isIncome': False, 'catTypeFilter': 'Personal', 'id': 65369843, 'ramt': 0, 'pi
': 11}, {'isTransfer': False, 'rbal': -31.55, 'st': 3, 'ex': False, 'isExpense'
 True, 'cat': 'Groceries', 'amt': 831.55, 'bgt': 800.0, 'type': 0, 'isIncome':
alse, 'catTypeFilter': 'Personal', 'id': 65369842, 'ramt': 0, 'pid': 7}, {'isTr
nsfer': False, 'rbal': 6.73, 'st': 2, 'ex': False, 'isExpense': True, 'cat': 'R
staurants', 'amt': 93.27, 'bgt': 100.0, 'type': 0, 'isIncome': False, 'catTypeF
lter': 'Personal', 'id': 65369840, 'ramt': 0, 'pid': 7}, {'isTransfer': False,
rbal': 38.0, 'st': 1, 'ex': False, 'isExpense': True, 'cat': 'Charity', 'amt':
, 'bgt': 38.0, 'type': 0, 'isIncome': False, 'catTypeFilter': 'Personal', 'id':
65369860, 'ramt': 0, 'pid': 8}, {'isTransfer': False, 'rbal': 60.0, 'st': 1, 'e
': False, 'isExpense': True, 'cat': 'Gift', 'amt': 0, 'bgt': 60.0, 'type': 0, '
sIncome': False, 'catTypeFilter': 'Personal', 'id': 65369857, 'ramt': 0, 'pid':
8}, {'isTransfer': False, 'rbal': 560.0, 'st': 1, 'ex': False, 'isExpense': Tru
, 'cat': 'Tithes and Offerings', 'amt': 0, 'bgt': 560.0, 'type': 0, 'isIncome':
False, 'catTypeFilter': 'Personal', 'id': 65369846, 'ramt': 0, 'pid': 8}, {'isT
ansfer': False, 'rbal': -12.0, 'st': 3, 'ex': False, 'isExpense': True, 'cat':
Unknown', 'amt': 112, 'bgt': 100.0, 'type': 0, 'isIncome': False, 'catTypeFilte
': 'Personal', 'id': 65369853, 'ramt': 0, 'pid': None}, {'roll': True, 'isTrans
er': False, 'rbal': 1150.0, 'st': 1, 'isIncome': False, 'ex': False, 'isExpense
: True, 'cat': 'Home Insurance', 'amt': -1100.0, 'bgt': 50.0, 'type': 0, 'catTy
eFilter': 'Personal', 'id': 65369848, 'ramt': -1100.0, 'pid': 12}, {'isTransfer
: False, 'rbal': 12.0, 'st': 1, 'ex': False, 'isExpense': True, 'cat': 'Home Se
vices', 'amt': 0, 'bgt': 12.0, 'type': 0, 'isIncome': False, 'catTypeFilter': '
ersonal', 'id': 65369849, 'ramt': 0, 'pid': 12}, {'isTransfer': False, 'rbal':
.09, 'st': 2, 'ex': False, 'isExpense': True, 'cat': 'Mortgage & Rent', 'amt':
395.91, 'bgt': 2400.0, 'type': 0, 'isIncome': False, 'catTypeFilter': 'Personal
, 'id': 65369854, 'ramt': 0, 'pid': 12}, {'isTransfer': False, 'rbal': -35.97,
st': 3, 'ex': False, 'isExpense': True, 'cat': 'Unknown', 'amt': 185.97, 'bgt':
150.0, 'type': 0, 'isIncome': False, 'catTypeFilter': 'Personal', 'id': 6536984
, 'ramt': 0, 'pid': None}, {'isTransfer': False, 'rbal': 75.0, 'st': 1, 'ex': F
lse, 'isExpense': True, 'cat': 'Hair', 'amt': 0, 'bgt': 75.0, 'type': 0, 'isInc
me': False, 'catTypeFilter': 'Personal', 'id': 65369861, 'ramt': 0, 'pid': 4},
'isTransfer': False, 'rbal': -15.57, 'st': 3, 'ex': False, 'isExpense': True, '
at': 'Unknown', 'amt': 165.57, 'bgt': 150.0, 'type': 0, 'isIncome': False, 'cat
ypeFilter': 'Personal', 'id': 65369844, 'ramt': 0, 'pid': None}, {'roll': True,
'isTransfer': False, 'rbal': 14122.0, 'st': 1, 'isIncome': False, 'ex': False,
isExpense': True, 'cat': 'Property Tax', 'amt': -13508.0, 'bgt': 614.0, 'type':
0, 'catTypeFilter': 'Personal', 'id': 65369841, 'ramt': -13508.0, 'pid': 19}],
income': [{'isTransfer': False, 'rbal': 1319.87, 'st': 1, 'ex': False, 'isExpen
e': False, 'cat': 'Unknown', 'amt': 6680.13, 'bgt': 8000.0, 'type': 0, 'isIncom
': True, 'catTypeFilter': 'Personal', 'id': 65369852, 'ramt': 0, 'pid': None}]}

这是我的脚本:

requests.put('https://api.myjson.com/bins/1quvq', data=str(budget))

但我不断收到 500 错误:

b'{"status":500,"message":"Internal Server Error","more_info":"undefined method`string\' for #\\u003cPhusionPassenger::Utils::TeeInput:0x007f83edc7eac0\\u003e"}'

我做错了什么?

最佳答案

好的,这对我有用:-

import requests

budget = {'s': 'd'}

r = requests.put('https://api.myjson.com/bins/1quvq', json=budget)
print r.content

如果您的数据仍有问题,您需要在问题中包含完整数据。你的字典的边缘似乎被截断了。

关于python - 在 Myjson.com API 上使用 Python Requests.put 时出现 500 服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33816665/

相关文章:

python - 如何使用默认情况下在 Python 中打印的包中的日志记录?

javascript - 搜索并查找以特定数字开头的数字

javascript - 当父项中的选择发生更改时,如何重置依赖子项的值?

android - Gridview——点击图片在Viewpager中查看图片

python - 在Python中迭代HTML中的 "class"属性?

python - 随机播放程序(列表索引超出范围错误)

api - 通过 API 的 Gmail 标签颜色?

java - Rally REST WS 查询 : Getting ObjectIDs of One-To-Many children using API v2. 0

python - Flask-restful API 不接受 json

PHP 和 python 节俭