TypeError:期望的字符串或缓冲区解码json在烧瓶中 [英] TypeError: expected string or buffer decoding json in flask

查看:226
本文介绍了TypeError:期望的字符串或缓冲区解码json在烧瓶中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个分布式系统,其中php应用程序向python flask应用程序发送一个post请求。

I'm working with a a distributed system where a php app sends a post request to a python flask app.

我有一个2d php数组($ data),如下所示:

I have a 2d php array ($data) that looks like:

array(3) { [0]=> array(2) { ["a"]=> 'aaa' ["token"]=> string(55) "146bf00b2cb8709" } [1]=> array(2) { ["a"]=> string(52) "bbb" ["token"]=> string(55) "146bf00b2cb96e74302" } [2]=> array(2) { ["a"]=> string(52) "ccc" ["token"]=> string(55) "146bf00b2cb96e6c422417" } }



< com / questions / 32335668 / how-to-send-2d-array-through-php-curl?noredirect = 1#32335775>如何通过php cURL发送2D数组

我有:

$json =  json_encode($data);
$ch = curl_init();  

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);    
$output=curl_exec($ch);

在烧瓶中,我的代码是:

In flask, my code is:

@app.route('/index',methods=['POST'])
def index():

    import json
    r = request # receives request from php
    json = json.loads(r.form)

我可以看到请求是以JSON形式传递的:

and I can see that the request is coming through as JSON:

>>> r.form
Out[12]: ImmutableMultiDict([('[{"a":"aaa","token":"146bf00b2cb96e6c425c2ab3f7daa11c55e4bc6eb5f2d5.46266037"},{"a":"bbb","token":"decdbc1691816cae83392e2a379a2c2555e4bc60df5f86.89400754"},{"a":"ccc","token":"146bf00b2cb96e6c425c2ab3f7daa11c55e4bc6e039320.76477875"}]', u'')])

但我似乎不能把它变成可解析的形式。我得到上面的错误。如何访问此JSON字符串?

But I can't seem to get it into a parsable form. I' getting the error above. How can I access this JSON string?

推荐答案

@app.route('/index',methods=['POST'])
def index():
    json = request.get_json()

这篇关于TypeError:期望的字符串或缓冲区解码json在烧瓶中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆