0

I have create a Flask Api (simple demo)

It will take a json and add those values and return addition.

Can anyone tell me how can i call that API with param?

please help me with this.

I have attached what I have done.


app = Flask(__name__)

@app.route('/add', methods=['POST'])
def add():
    data = request.get_json()
    return jsonify({'sum': data['x'] + data['x']})

app.run(port=5003)

when i used below code it gives an error OSError: [Errno 98] Address already in use

I used this code from diff .py file

url='http://127.0.0.1:5003/'
response = app.test_client().post(
        '/add',
        data=json.dumps({'x': 1, 'y': 2}),
        content_type='application/json',
    )

I want to call that API from diff .py file, this is what i want.

Thanks

jony
  • 924
  • 10
  • 25
  • Not sure about the error, but `app.test_client().post(url+'/add' ...)` in `diff.py` seems wrong. You already have a trailing slash in the `url` string variable. So maybe you want to do `url + 'add'` instead? – Jake Tae Jun 14 '20 at 19:43
  • @JakeTae by mistake!, updated – jony Jun 14 '20 at 19:44
  • It looks like you are running another application on port `5003`. You need to stop that application first and then run the flask app – Sirajus Salekin Jun 14 '20 at 20:55
  • @SirajusSalekin , if i stop it, then how can i call that API and check whether it is running or not – jony Jun 14 '20 at 20:58
  • 1
    you are trying to run it twice somewhere in your code – Sirajus Salekin Jun 14 '20 at 21:17
  • @SirajusSalekin I got sol here https://stackoverflow.com/questions/20001229/how-to-get-posted-json-in-flask – jony Jun 15 '20 at 08:00

0 Answers0