2

我正在尝试使用 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
4

3 回答 3

1

如果您将架构更改为强制将值转换为布尔值deleted,您可以发送intstr值并在插入/更新时将其转换为布尔值

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

to_bool = lambda v: v if type(v) is bool else str(v).lower() in ['true', '1']

然后,更改deleted架构中的 以使用该函数来强制值,如下所示:

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

有了这个,您可以例如发送带值的已删除,例如'0',或yelding 到布尔 false,或, ,或导致 true0'false''False''1'1'true''True'

于 2015-12-29T19:12:27.720 回答
0

仅当您以 form-data 或 的内容类型发送请求时,才会发生这种情况application/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"
}) 
于 2016-11-10T12:53:26.133 回答
-1

你的有效载荷应该是一个像这样的字典:

payload = {"somebool": False}

然后将其转换为 json 字符串:

payload_json = json.dumps(payload)

这导致字符串中的小写布尔值:

'{"f5only": false}'

那么你应该能够发布或修补它:

requests.post("{0}/endpoint/".format(api_server), headers=headers, data=payload_json)
于 2015-06-08T15:08:14.163 回答