Python/Bottle/MongoDB:不支持的响应类型:

发布于 2025-01-07 09:42:32 字数 507 浏览 5 评论 0 原文

@route('/locations', method='GET')
def get_location():
    entity = db['locations'].find({'coordinate2d': {'$near': [37.871593, -122.272747]}}).limit(3)
    if not entity:
        abort(404, 'No nearby locations')
    return entity

上述代码部分的响应是:

Error 500: Internal Server Error

Sorry, the requested URL 'http://localhost:8080/locations' caused an error:

Unsupported response type: <type 'dict'>

如何从 mongo 获取信息作为 Bottle 可以返回 JSON 的类型?

@route('/locations', method='GET')
def get_location():
    entity = db['locations'].find({'coordinate2d': {'$near': [37.871593, -122.272747]}}).limit(3)
    if not entity:
        abort(404, 'No nearby locations')
    return entity

The response for the above portion of code is:

Error 500: Internal Server Error

Sorry, the requested URL 'http://localhost:8080/locations' caused an error:

Unsupported response type: <type 'dict'>

How can I grab that information from mongo as a type Bottle can return as JSON?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最近可好 2025-01-14 09:42:32

完整的解决方案是将数据库游标转换为列表、手动设置响应类型+自定义编码返回值的组合

@route('/locations/:lat/:lng', method='GET')
def get_location(lat,lng):
    response.content_type = 'application/json'
    objdb = db.locations.find({'coordinate2d': {'$near': [lat,lng]}}, {'coordinate2d':bool(1)}).skip(0).limit(3)
    entries = [entry for entry in objdb]
    return MongoEncoder().encode(entries)

在我的例子中,产生以下结果:

[
    {
        "_id": "4f4201bb7e720d1dca000005",
        "coordinate2d": [
            33.02032100000006,
            -117.19483074631853
        ]
    },
    {
        "_id": "4f4201587e720d1dca000002",
        "coordinate2d": [
            33.158092999999994,
            -117.350594
        ]
    },
    {
        "_id": "4f42018b7e720d1dca000003",
        "coordinate2d": [
            33.195870000000006,
            -117.379483
        ]
    }
]

The complete solution was a combination of transforming the db cursor to a list, manually setting the response type + custom encoding the return value

@route('/locations/:lat/:lng', method='GET')
def get_location(lat,lng):
    response.content_type = 'application/json'
    objdb = db.locations.find({'coordinate2d': {'$near': [lat,lng]}}, {'coordinate2d':bool(1)}).skip(0).limit(3)
    entries = [entry for entry in objdb]
    return MongoEncoder().encode(entries)

In my case, produces this:

[
    {
        "_id": "4f4201bb7e720d1dca000005",
        "coordinate2d": [
            33.02032100000006,
            -117.19483074631853
        ]
    },
    {
        "_id": "4f4201587e720d1dca000002",
        "coordinate2d": [
            33.158092999999994,
            -117.350594
        ]
    },
    {
        "_id": "4f42018b7e720d1dca000003",
        "coordinate2d": [
            33.195870000000006,
            -117.379483
        ]
    }
]
心清如水 2025-01-14 09:42:32

当我尝试返回 python 列表时出现此错误。我以为它会转换为 JSON,但事实并非如此。它到达了 Bottle.py 中处理可迭代对象的行,并找到了列表中的第一个字典并抛出了上面的错误。

为了解决这个问题,我只是将列表嵌入到字典中。

return {'response': []}

I got this error when I was trying to return a python list. I assumed it would translate into JSON, but it didn't. It made it to the line in bottle.py where it would handle iterables and found the first dict in the list and threw the error above.

To get around this, I simply embedded my list inside a dict.

return {'response': []}
悲喜皆因你 2025-01-14 09:42:32

根据瓶子上的文档提到 http://bottlepy.org/docs/dev/ 你有从 @route 装饰器返回字符串。您必须返回带有数据或字符串的模板。

如果您想生成 json,则必须更改 Content-Type

字典

如上所述,Python 字典(或子类
其中)自动转换为JSON字符串并返回
将 Content-Type 标头设置为 application/json 的浏览器。
这使得实现基于 json 的 API 变得容易。数据格式其他
也支持 json 。请参阅教程-输出-过滤器进行学习
更多。

http://bottlepy.org/docs/dev/tutorial。 html?highlight=json#生成内容

As per the doc mention on bottle http://bottlepy.org/docs/dev/ you have to return the string from the @route decorator. You have to return the template with data or string.

If you want to generate the json then you have to change the Content-Type.

Dictionaries

As mentioned above, Python dictionaries (or subclasses
thereof) are automatically transformed into JSON strings and returned
to the browser with the Content-Type header set to application/json.
This makes it easy to implement json-based APIs. Data formats other
than json are supported too. See the tutorial-output-filter to learn
more.

http://bottlepy.org/docs/dev/tutorial.html?highlight=json#generating-content

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文