Wilson Reuben
Wilson Reuben

Reputation: 63

Kivy UrlRequest not passing data

I am writing a flask backend with flask sqlalchemy and marshmallow which returns json and kivy as frontend. I am trying to login in my flask api using kivy urlrequest but no data is been passed. I get error from my flask api ('no input data provided') even so i passed valid data.

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':'[email protected]', '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)

please how to fix this?

Upvotes: 2

Views: 1606

Answers (1)

Wilson Reuben
Wilson Reuben

Reputation: 63

And to answer my question

My API only accepts json data and i never passed one just plain text. To fix this, i called json.dumps to convert the data into json and changed the Content-type from application/x-www-form-urlencoded to application/json

below is the fixed code:

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':'[email protected]', '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)

Upvotes: 2

Related Questions