json-rpc 格式错误的请求  

发布于 2024-11-01 04:13:28 字数 875 浏览 0 评论 0原文

我正在构建一个使用 json-rpc 与 django 后端通信的应用程序。到目前为止,一切进展顺利。但是我发现发送“ ”时出现异常。据我所知,该请求工作正常,但是 django 对响应的解释很糟糕。我在下面复制了一个简化的请求和响应:

请求:

{"jsonrpc":"2.0","id":"1","method":"test","params":
{"id":"80","name":"tests","introduction":"hello  there"}}

Django 接收:

<QueryDict:u'{"jsonrpc":"2.0","id":"1","method":"test","params":
{"id":"80","name":"tests","introduction":"hello ': [u''], u'nbsp': [u''], u'there"}}': [u'']}>

预期响应:

<QueryDict: {u'{"jsonrpc":"2.0","id":"1","method":"test","params":
{"id":"80","name":"tests","introduction":"hello &nbsp;there"}}': [u'']}>

看起来 django 解释了 & 。和;作为特殊字符,因此在其 request.POST 变量中创建了一个意外的字典。

我需要做什么来确保 json 字符串不会格式错误?我尝试使用 php htmlspecialchars() 方法对其进行编码,但因为这不会删除 '&'问题仍然存在。

任何帮助将不胜感激。

I'm building an application that communicates with a django backend using json-rpc. So far all has been working well. However I've found an anomaly in sending " ". As far as I know the request works fine, however django interprets the response badly. I've reproduced a simplified request and response below:

Request:

{"jsonrpc":"2.0","id":"1","method":"test","params":
{"id":"80","name":"tests","introduction":"hello  there"}}

Django receives:

<QueryDict:u'{"jsonrpc":"2.0","id":"1","method":"test","params":
{"id":"80","name":"tests","introduction":"hello ': [u''], u'nbsp': [u''], u'there"}}': [u'']}>

Expected response:

<QueryDict: {u'{"jsonrpc":"2.0","id":"1","method":"test","params":
{"id":"80","name":"tests","introduction":"hello  there"}}': [u'']}>

It seems like django interprets the & and the ; as special characters and so creates an unexpected dictionary in its request.POST variable.

What do I need to do to make sure that the json string doesn't get malformed? I have tried encoding it using the php htmlspecialchars() method, but since that doesn't remove the '&' the problem persists.

Any help will be much appreciated.

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

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

发布评论

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

评论(1

苍暮颜 2024-11-08 04:13:28

Django 通过解码主体(您的 json 字符串)来处理(POST?)请求,就好像它是一个查询字符串,而不是一个 json。

在查询字符串中,&; 表示键:值对的结尾。将请求正文拆分为这两个字符会生成您在 Django QueryDict 中看到的键:值对。

您需要获取 POST 请求正文,并使用标准 lib json 或 simplejson 模块自行将其显式解码为字典。

我对 Django 没有什么特别的经验,但我想在你的视图处理程序中的某个地方你会做一些类似的事情:

try:
    data = json.loads(requesst.raw_post_data)
    ## work with the data...
except ValueError:
    ## do something...

毫无疑问,Django 提供了一种方法来将这个 json 处理移出你的视图,并移动到更合适的地方。

Django is handling the (POST?) request by decoding the body (your json string) as if it were a query string, and not a json.

Within a query string, & and ; denote the end of a key:value pair. Splitting up your request body on those two characters yields the key:value pairs you see in the Django QueryDict.

You need to get hold of the POST request body and explicitly decode it to a dict yourself using either the standard lib json, or simplejson module.

I have little experience with Django specifically, but I imagine that somewhere in your view handler you would do something akin to:

try:
    data = json.loads(requesst.raw_post_data)
    ## work with the data...
except ValueError:
    ## do something...

No doubt Django provides a way to move this json handling out of your views, and to somewhere more suitable.

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