How do You simulate Requests using Already placed Json Data with python/flask
Question:
New to this, How do you simulate a HTTP Request with data which is already json and send it thru requests in python. I am trying to do it and its telling me, JSON Not serializable.
these are the lines of code which I am using
@app.route('/api/create-virtual-account',methods=['POST'])
@jwt_required()
def create_virtual_account():
current_user = get_jwt_identity()
_json = request.json
_account_name = _json['account_name']
_email = current_user
_bvn = _json['bvn']
_tx_ref = 'VAL12'
_phonenumber = _json['phonenumber']
_firstname = _json['firstname']
_lastname = _json['lastname']
_narration = _json['narration']
url = 'URL HERE'
data = {'account_name':_account_name,'email':_email,'bvn':_bvn,'phonenumber':_phonenumber,'firstname':_firstname,'lastname':_lastname,'narration':_narration}
res = requests.post(url, data=json.dumps(data),headers={'Content-Type':'application/json','Authorization':'Bearer FLWSECK_TEST-72fe360edef17334f4817a17407011bb-X'})
return jsonify({'result':res.content}),200
I know there is something i am missing, only i am not sure what it is. I am fairly new to this, so I need guidance
This is the Error i am getting
Press CTRL+C to quit
[2022-10-28 20:43:12,650] ERROR in app: Exception on /api/create-virtual-account [POST]
Traceback (most recent call last):
File "C:Program FilesPython310libsite-packagesflaskapp.py", line 2525, in wsgi_app
response = self.full_dispatch_request()
File "C:Program FilesPython310libsite-packagesflaskapp.py", line 1822, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:UsersemiAppDataRoamingPythonPython310site-packagesflask_corsextension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "C:Program FilesPython310libsite-packagesflaskapp.py", line 1820, in full_dispatch_request
rv = self.dispatch_request()
File "C:Program FilesPython310libsite-packagesflaskapp.py", line 1796, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "C:UsersemiAppDataRoamingPythonPython310site-packagesflask_jwt_extendedview_decorators.py", line 154, in decorator
return current_app.ensure_sync(fn)(*args, **kwargs)
File "D:flask_rest_api_tut****main.py", line 177, in create_virtual_account
return jsonify({'result':res.content}),200
File "C:Program FilesPython310libsite-packagesflaskjson__init__.py", line 342, in jsonify
return current_app.json.response(*args, **kwargs)
File "C:Program FilesPython310libsite-packagesflaskjsonprovider.py", line 309, in response
f"{self.dumps(obj, **dump_args)}n", mimetype=mimetype
File "C:Program FilesPython310libsite-packagesflaskjsonprovider.py", line 230, in dumps
return json.dumps(obj, **kwargs)
File "C:Program FilesPython310libjson__init__.py", line 238, in dumps
**kw).encode(obj)
File "C:Program FilesPython310libjsonencoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:Program FilesPython310libjsonencoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:Program FilesPython310libsite-packagesflaskjsonprovider.py", line 122, in _default
raise TypeError(f"Object of type {type(o).__name__} is not JSON serializable")
TypeError: Object of type bytes is not JSON serializable
127.0.0.1 - - [28/Oct/2022 20:43:12] "POST /api/create-virtual-account HTTP/1.1" 500 -
Answers:
Instead of using res.content
use res.json()
. .json()
method will parse the Json response to Python objects so you can use jsonify()
method without error:
...
return jsonify({'result':res.json()}),200
New to this, How do you simulate a HTTP Request with data which is already json and send it thru requests in python. I am trying to do it and its telling me, JSON Not serializable.
these are the lines of code which I am using
@app.route('/api/create-virtual-account',methods=['POST'])
@jwt_required()
def create_virtual_account():
current_user = get_jwt_identity()
_json = request.json
_account_name = _json['account_name']
_email = current_user
_bvn = _json['bvn']
_tx_ref = 'VAL12'
_phonenumber = _json['phonenumber']
_firstname = _json['firstname']
_lastname = _json['lastname']
_narration = _json['narration']
url = 'URL HERE'
data = {'account_name':_account_name,'email':_email,'bvn':_bvn,'phonenumber':_phonenumber,'firstname':_firstname,'lastname':_lastname,'narration':_narration}
res = requests.post(url, data=json.dumps(data),headers={'Content-Type':'application/json','Authorization':'Bearer FLWSECK_TEST-72fe360edef17334f4817a17407011bb-X'})
return jsonify({'result':res.content}),200
I know there is something i am missing, only i am not sure what it is. I am fairly new to this, so I need guidance
This is the Error i am getting
Press CTRL+C to quit
[2022-10-28 20:43:12,650] ERROR in app: Exception on /api/create-virtual-account [POST]
Traceback (most recent call last):
File "C:Program FilesPython310libsite-packagesflaskapp.py", line 2525, in wsgi_app
response = self.full_dispatch_request()
File "C:Program FilesPython310libsite-packagesflaskapp.py", line 1822, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:UsersemiAppDataRoamingPythonPython310site-packagesflask_corsextension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "C:Program FilesPython310libsite-packagesflaskapp.py", line 1820, in full_dispatch_request
rv = self.dispatch_request()
File "C:Program FilesPython310libsite-packagesflaskapp.py", line 1796, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "C:UsersemiAppDataRoamingPythonPython310site-packagesflask_jwt_extendedview_decorators.py", line 154, in decorator
return current_app.ensure_sync(fn)(*args, **kwargs)
File "D:flask_rest_api_tut****main.py", line 177, in create_virtual_account
return jsonify({'result':res.content}),200
File "C:Program FilesPython310libsite-packagesflaskjson__init__.py", line 342, in jsonify
return current_app.json.response(*args, **kwargs)
File "C:Program FilesPython310libsite-packagesflaskjsonprovider.py", line 309, in response
f"{self.dumps(obj, **dump_args)}n", mimetype=mimetype
File "C:Program FilesPython310libsite-packagesflaskjsonprovider.py", line 230, in dumps
return json.dumps(obj, **kwargs)
File "C:Program FilesPython310libjson__init__.py", line 238, in dumps
**kw).encode(obj)
File "C:Program FilesPython310libjsonencoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:Program FilesPython310libjsonencoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:Program FilesPython310libsite-packagesflaskjsonprovider.py", line 122, in _default
raise TypeError(f"Object of type {type(o).__name__} is not JSON serializable")
TypeError: Object of type bytes is not JSON serializable
127.0.0.1 - - [28/Oct/2022 20:43:12] "POST /api/create-virtual-account HTTP/1.1" 500 -
Instead of using res.content
use res.json()
. .json()
method will parse the Json response to Python objects so you can use jsonify()
method without error:
...
return jsonify({'result':res.json()}),200