Kivy UrlRequest没有传递数据

时间:2017-05-23 19:33:40

标签: python flask kivy

我正在用烧瓶sqlalchemy和marshmallow写一个烧瓶后端,它将json和kivy作为前端返回。我正在尝试使用kivy urlrequest登录我的flask api,但没有传递任何数据。我从我的烧瓶api('没有提供输入数据')得到错误,即使我传递了有效数据。

from kivy.network.urlrequest import UrlRequest
import urllib
import json

def success(req, result):
    print('success')

def fail(req, result):
    print('fail')

def error(req, result):
    print('error')

def progress(req, result, chunk):
    print('loading')

values = {'email':'wilson@gmail.com', 'pwdhash':'wilson'}
params = urllib.urlencode(values)
headers = {'Content-type': 'application/x-www-form-urlencoded',
          'Accept': 'text/plain'}

req = UrlRequest('http://127.0.0.1:5000/login', on_success=success, on_failure=fail, on_error=error, on_progress=progress, req_body=params, req_headers=headers)
req.wait()
print(req.result)

请问如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

回答我的问题

我的API只接受json数据,我从未传递过纯文本。为了解决这个问题,我调用json.dumps将数据转换为json,并将Content-type从application / x-www-form-urlencoded更改为application / json

下面的

是固定代码:

from kivy.network.urlrequest import UrlRequest
import urllib
import json

def success(req, result):
    print('success')

def fail(req, result):
    print('fail')

def error(req, result):
    print('error')

def progress(req, result, chunk):
    print('loading')

values = {'email':'wilson@gmail.com', 'pwdhash':'wilson'}
# converted data to json type
params = json.dumps(values)

# changed Content-type from application/x-www-form-urlencoded to application/json
headers = {'Content-type': 'application/json',
          'Accept': 'text/plain'}

req = UrlRequest('http://127.0.0.1:5000/login', on_success=success, on_failure=fail, on_error=error, on_progress=progress, req_body=params, req_headers=headers)
req.wait()
print(req.result)