I have below code where I am making an API using flask:
from flask import Flask, request, jsonify
import json
app = Flask(__name__)
@app.route('/post', methods=['GET','POST'])
def post():
payload = request.data
# return jsonify(data)
# json_object = json.dumps(data, indent=4)
return payload
# return jsonify(payload)
if __name__ == '__main__':
app.run(debug=True)
and using below code to call the localhost hosted API:
import requests
headers = {'Content-Type': 'application/json; charset=utf-8'}
url = "http://localhost:5000/post"
text = {"filename": "my_file.csv"}
response = requests.post(url, json=text , headers=headers )
if response.status_code == 200:
print("CSV file is valid")
# print(data)
# print(getjson)
print(text)
else:
print("CSV file is invalid")
The problem is
that when I call the localhost URL using the calling code I get a successful connection, however no data gets "posted" on the URL,
if I try to jsonify (or use JSON dumps) and then run the calling code it stills runs successfully however the localhost site gives below error:
Bad Request
Did not attempt to load JSON data because the request Content-Type was not 'application/json'.
How can I rectify this? My aim is to push JSON using the flask API and display it on the localhost site. The long term goal being is that we need to create an app that is validating the JSON passed, so to begin with I am trying to see if it is reading the JSON successfully as-is, or not?