所以我有一个Flask应用程序,它使用Flask-Restless和Flask-SQLAlchemy模块来创建API。我的GET请求工作正常,但POST请求不是。
来自models.py:
from application import db
class User(db.Model):
__bind_key__= 'users'
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(80))
def __init__(self, email):
self.email = email
来自application.py(API设置):
from flask.ext.sqlalchemy import SQLAlchemy
import flask.ext.restless
db = SQLAlchemy(application)
manager = flask.ext.restless.APIManager(application, flask_sqlalchemy_db=db)
manager.create_api(User, methods=['GET', 'POST'])
请求进程对API的请求很好,但是当我有这样的POST请求时:
import json
import requests
url = 'http:<ip>:<port>/api/user'
data = {'email': '1234@here.com'}
headers = {'Content-type': 'application/json'}
r = requests.post(url, data=json.dumps(data), headers=headers)
我收到此错误:
ERROR in views [X:\Python27\lib\site-packages\flask_restless\views.py:413]:
need more than 1 value to unpack
--------------------------------------------------------------------------------
Traceback (most recent call last):
File "X:\Python27\lib\site-packages\flask_restless\views.py", line 409, in extract_error_messages
left, right = str(exception).rsplit(':', 1)
ValueError: need more than 1 value to unpack
我知道这与我发送的json&f电子邮件数据有关,但我很难找到它想要的内容。任何帮助表示赞赏!
答案 0 :(得分:0)
我找到了解决方案!
在models.py下:
def __init__(self, email):
电子邮件(以及任何其他模型的参数)需要=无
def __init__(self, email=None):
之后,此代码可以使用
import requests
url = 'http:<ip>:<port>/api/user'
data = {'email': '1234@here.com'}
r = requests.post(url, json=data)