Rakesh Kumar
Rakesh Kumar

Reputation: 45

POST data from Python script to Flask

I am trying to send data from a python script to flask endpoint. I have tried many things but I don't understand where I am going wrong. Please let me know if you have a solution or any reference that can help me.

Thanks in advance

This is the Flask code which gets json data:

@app.route('/ramp-DynDB-DQ-PostTableItem', methods=['POST' , 'GET'])
def PostDQTableItem():
    data = request.get_json()
    print(data)

This is the Python Script that Posts data:

 import requests

newHeaders = {'Content-type': 'application/json', 'Accept': 'text/plain'}
response = requests.post('http://blabla:5000/ramp-DynDB-DQ-PostTableItem',
                        data={"Key": "key", "Path": "path"},
                        headers=newHeaders)

Status code:

400

Upvotes: 0

Views: 803

Answers (1)

Rakesh Kumar
Rakesh Kumar

Reputation: 45

Converting Dict to JSON format worked for me:

import requests, json

data = {"Key": "key", 
        "Path": "path"
        }

json_object = json.dumps(data, indent = 4)   
print(json_object) 
newHeaders = {'Content-type': 'application/json', 'Accept': 'text/plain'}
res = requests.get('http://blabla/ramp-DynDB-DQ-PostTableItem')
response = requests.post('http://blabla/ramp-DynDB-DQ-PostTableItem',
                        data=json_object,
                        headers=newHeaders)
print(response.text)

Upvotes: 1

Related Questions