Python Eve - POST 包含列表的负载
Python Eve - POST payload containing a list
我在使用模式中的 list
类型时遇到问题。每当我尝试 POST 时,我都会收到一个 422 响应,说明 'must be of list type'。下面是一个产生这个问题的简单例子。
from eve import Eve
people = {
'schema': {
'tests': {
'type': 'list',
'schema': {
'type': 'string'
},
'required': True,
}
},
'resource_methods': ['GET', 'POST'],
}
settings = {
'DOMAIN': {
'people': people
}
}
app = Eve(settings=settings)
if __name__ == '__main__':
app.run()
然后当您 POST 使用以下内容连接到人员端点时:
import requests
url = "http://localhost:5000/people"
person = {
"tests": ['a', 'b'],
}
r = requests.post(url, data=person)
print(r.json())
您收到 422 响应。当我对此进行调试时,看起来 Eve 应用程序收到的 tests
参数只是一个字符串 'a'
,而不是整个列表。从我在 GitHub 的 Eve 测试中看到的情况来看,这似乎是发出请求的正确方法,所以我只能假设我在设置 resource/schema 时犯了错误?
谢谢。
如果你打印 request.POST
,你会看到 UnicodeMultiDict([('tests', u'a'), ('tests', u'b')])
。解决此问题的方法是为您的 post
.
使用 json object
person = json.dumps({
"tests": ['a', 'b'],
})
r = requests.post(url, json=person)
print(r.json())
或者在您的情况下,您将不得不以某种方式在 API 端调整您的 POST 请求以获得列表:-
request.POST.getall('tests')
然后继续。
请检查using json in POST request。此外,在使用 json
时,可能不需要 json.dumps,字典将自动为 jsonified
。
我在使用模式中的 list
类型时遇到问题。每当我尝试 POST 时,我都会收到一个 422 响应,说明 'must be of list type'。下面是一个产生这个问题的简单例子。
from eve import Eve
people = {
'schema': {
'tests': {
'type': 'list',
'schema': {
'type': 'string'
},
'required': True,
}
},
'resource_methods': ['GET', 'POST'],
}
settings = {
'DOMAIN': {
'people': people
}
}
app = Eve(settings=settings)
if __name__ == '__main__':
app.run()
然后当您 POST 使用以下内容连接到人员端点时:
import requests
url = "http://localhost:5000/people"
person = {
"tests": ['a', 'b'],
}
r = requests.post(url, data=person)
print(r.json())
您收到 422 响应。当我对此进行调试时,看起来 Eve 应用程序收到的 tests
参数只是一个字符串 'a'
,而不是整个列表。从我在 GitHub 的 Eve 测试中看到的情况来看,这似乎是发出请求的正确方法,所以我只能假设我在设置 resource/schema 时犯了错误?
谢谢。
如果你打印 request.POST
,你会看到 UnicodeMultiDict([('tests', u'a'), ('tests', u'b')])
。解决此问题的方法是为您的 post
.
json object
person = json.dumps({
"tests": ['a', 'b'],
})
r = requests.post(url, json=person)
print(r.json())
或者在您的情况下,您将不得不以某种方式在 API 端调整您的 POST 请求以获得列表:-
request.POST.getall('tests')
然后继续。
请检查using json in POST request。此外,在使用 json
时,可能不需要 json.dumps,字典将自动为 jsonified
。