from flask import Flask, abort, request
    import json
    app = Flask(__name__)


    @app.route('/data', methods=['POST'])
    def data():
        if not request.json:
            abort(400)
        print(request.json)
        return json.dumps(request.json)


    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=5001, debug=True)

i want to send all my post request to another endpoint at localhost:8000.. please suggest the correctionsenter code here

1 Answers

0
Zik On Best Solutions

I tried run your code, it works fine, here is my exmaple:

Server same as yours. One suggestion about your code: .json attribute has been deprecated, you should use the request.get_json() method.

   from flask import Flask, abort, request
    import json
    app = Flask(__name__)


    @app.route('/data', methods=['POST'])
    def data():
        if not request.json:
            abort(400)
        print(request.json)
        return json.dumps(request.json)


    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=5001, debug=True)

client:

import requests
res = requests.post('http://localhost:5001/data', json={"mytext":"test"})
if res.ok:
    print(res.json())

I guessed you don't fire the right HTTP request. for exmaple, do you add attribute Content-Type: application/json in your http header?