Rest 如何将布尔值发送到python eve?

Rest 如何将布尔值发送到python eve?,rest,eve,Rest,Eve,我尝试使用python eve更新布尔值,但总是收到相同的错误 _issues: {deleted:must be of boolean type} deleted: "must be of boolean type" _status: "ERR" 我已经尝试将字段发送为true(设置javascript类型)'true'和'true'(作为文本)和1,但错误总是一样的 startdate=2014-03-25T03%3A00%3A00.000Z&code=dsa&enddat

我尝试使用python eve更新布尔值,但总是收到相同的错误

_issues: {deleted:must be of boolean type}
deleted: "must be of boolean type"
_status: "ERR"
我已经尝试将字段发送为true(设置javascript类型)'true'和'true'(作为文本)和1,但错误总是一样的

startdate=2014-03-25T03%3A00%3A00.000Z&code=dsa&enddate=2014-03-31T03%3A00%3A00.000Z&name=sad&note=&deleted=True

startdate=2014-03-25T03%3A00%3A00.000Z&code=dsa&enddate=2014-03-31T03%3A00%3A00.000Z&name=sad&note=&deleted=true

startdate=2014-03-25T03%3A00%3A00.000Z&code=dsa&enddate=2014-03-31T03%3A00%3A00.000Z&name=sad&note=&deleted=1
有什么想法吗

问候

加斯顿

设置.py

entity= {
    'resource_methods': ['GET', 'POST'],
    'schema': schema,
    'datasource': {
            'filter': {'$or':[{'deleted':{'$exists':0}},{'deleted':False}]}
            }
}

schema = {
'name': {
    'type': 'string',
    'minlength': 1,
    'maxlength': 50,
    'required': True,
},
'code': {
    'type': 'string',
},
'deleted':{
    'type':'boolean',
    'default':False
}
}
完整请求

Request URL:http://localhost:5000/campaign/532f797da54d75faabdb25d5
Request Method:PUT
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,no;q=0.6,es;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Content-Length:112
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:localhost:5000
If-Match:3c7bc93e3c7d60da62f350ac990c16e29b08660f
Origin:http://localhost:5000
Pragma:no-cache
Referer:http://localhost:5000/static/index.html
X-Requested-With:XMLHttpRequest
Form Dataview parsed
startdate=2014-03-25T03%3A00%3A00.000Z&code=dsa&enddate=2014-03-31T03%3A00%3A00.000Z&name=sad&note=&deleted=True
Response Headersview source
Access-Control-Allow-Headers:
Access-Control-Allow-Max-Age:21600
Access-Control-Allow-Methods:HEAD, GET, PUT, POST, DELETE, OPTIONS, PATCH
Access-Control-Allow-Origin:*
Content-Length:69
Content-Type:application/json
Date:Mon, 24 Mar 2014 00:30:10 GMT
Server:Werkzeug/0.9.4 Python/2.7.5
Set-Cookie:session=eyJfcGVybWFuZW50Ijp0cn

您的有效负载应该是一个类似于:

payload = {"somebool": False}
然后将其转换为json字符串:

payload_json = json.dumps(payload)
这将导致字符串中出现小写布尔值:

'{"f5only": false}'
然后您应该能够发布或修补它:

requests.post("{0}/endpoint/".format(api_server), headers=headers, data=payload_json)

如果您将模式更改为强制将
已删除
值转换为bool,则可以发送
int
str
值,并在插入/更新时将其转换为bool

首先,创建一个函数,将出现的任何内容转换为bool:

to_bool = lambda v: v if type(v) is bool else str(v).lower() in ['true', '1']
然后,在模式中更改
已删除的
,以使用函数强制值,如下所示:

'deleted':{
    'type':'boolean',
    'coerce': to_bool,
    'default':False
}

使用此选项,您可以在每个示例中使用诸如
'0'
0
'false'
'false'
的值将deleted发送到布尔值false,或
'1'
1
'true'
'true'
导致true

只有在表单数据或
应用程序/x-www-form-urlencoded
的内容类型中发送请求时才会发生这种情况。对于AJAX请求,发送布尔值是有效的

var payload = {
    "deleted": false
};

var xhr = $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(payload),
    dataType: "json",
    url: "some url"
})