Eve + MongoDB具有特殊字符

时间:2019-07-16 18:17:37

标签: python mongodb shell urlencode eve

我有一个(Mongo)数据库,其中包含来自多个行星/月球/小行星的位置。 我的数据库名为nomenclature,集合为centroids。 这是此集合中的文档示例:

[
{
  "name":"kachina chasmata",
  "location":{
    "type":"Point",
    "coordinates":[-116.65,-32.6]
  },
  "body":"ariel"
},
{
  "name":"hokusai",
  "location":{
    "type":"Point",
    "coordinates":[16.65,57.84]
  },
  "body":"mercury"
},
{
  "name":"cañas",
  "location":{
    "type":"Point",
    "coordinates":[89.86,-31.188]
  },
  "body":"mars"
},
{
  "name":"anseris cavus",
  "location":{
    "type":"Point",
    "coordinates":[95.5,-29.708]
  },
  "body":"mars"
}
]

此类数据库/集合将在其bodyname字段上接收查询。

您可能已经注意到ñ)中的空白和特殊字符(“ name”) 某些文档。< / em>这正是我的问题所在。

我正在使用eve通过只读(GET)接口发布此数据库/集合。 在设置中添加以下DOMAIN

DOMAIN = {
    'centroids': {
        'item_title': 'crater centroid',
        'url': 'centroid/<regex("[\w]+"):body>/<regex("[\w ]+"):name>'
    }
}

,夏娃可以很好地回答以下请求:

$ curl 'http://127.0.0.1:5000/centroid/mercury/hokusai'

$ curl 'http://127.0.0.1:5000/centroid/mars/anseris%20cavus'

name中有空格时(请注意 name <regex("[\w ]+"):name>的设置中的空格)。

问题是:在这样的环境中,我应该如何处理特殊字符(如ñ)?谁应该处理编码/解码:用户,界面(Eve)还是数据库(MongoDB)?

1 个答案:

答案 0 :(得分:0)

好的,我明白了。当我通过浏览器测试该应用程序时,我发现了一切正常的地方。我将在此处保留此问题,并在此处添加答案,因为这可能对某人有用;我学到了,希望其他人也可以。

TL; DR: 客户端负责编码查询,然后解码结果如有必要。

使用命令行进行第一次测试后,我通过Web浏览器完成了该操作,其中URL ##[section]Starting: build ============================================================================== Task : Bash Description : Run a Bash script on macOS, Linux, or Windows Version : 3.151.2 Author : Microsoft Corporation Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/bash ============================================================================== Generating script. ========================== Starting Command Output =========================== [command]/usr/bin/bash --noprofile --norc (path)be642b86-51b1-44c5-8727-71cc18ec0678.sh 2.2.300 Microsoft (R) Build Engine version 16.1.76+g14b0a930a7 for .NET Core Copyright (C) Microsoft Corporation. All rights reserved. Restore completed in 2.22 sec for (ProjA) ... Build FAILED. ... 7 Warning(s) 3 Error(s) Time Elapsed 00:00:05.73 Microsoft (R) Build Engine version 16.1.76+g14b0a930a7 for .NET Core Copyright (C) Microsoft Corporation. All rights reserved. Restore completed in 568.52 ms for (ProjB) ... Build succeeded. 0 Warning(s) 0 Error(s) Time Elapsed 00:00:03.17 ##[section]Finishing: build 在被app / db(Eve / MongoDB)接受时没有问题,并给出了答案:

http://localhost:5000/centroid/mars/cañas

太好了。但是现在我想知道如何从终端/重击中做到这一点。

首先,我在Google上搜索了“ URL编码”,并使用了这个小虱子工具:https://meyerweb.com/eric/tools/dencoder/。哪个为我编码了<resource> <resource> <body>mars</body> <lat>-31.188</lat> <lon>89.86</lon> <name>cañas</name> </resource> </resource> (它使用Javascript encodeURIComponent()),现在我可以尝试使用cañas

curl

好。答案在$ curl -s 'http://127.0.0.1:5000/centroid/mars/ca%C3%B1as%0A' | json_pp { "_items" : [ { "lat" : -31.188, "body" : "mars", "lon" : 89.86, "name" : "ca�as" } ] } 中。

然后我想自己在终端中进行编码。 现在进行第二次搜索,“编码utf8 bash”使我进入了这篇文章:https://www.tecmint.com/convert-files-to-utf-8-encoding-in-linux/,在这里我了解了ISO-8859工具。

使用iconv我有一个不同的编码字符串-我对iconv做了UTF-8,但是一切正常:

ISO-8859-1