0

I simplify my requirement to this simple code

from flask import Flask, request
import requests

app = Flask(__name__)

@app.route('/', methods=['POST'])
def root():
    payload = {'Message': 'yo','Port':'123'}
    r = requests.post("http://127.0.0.1:5000/test", data=payload)
    return r.text


@app.route('/test', methods=['POST'])
def test():
    return request.form['Message']+','+request.form['Port']

if __name__ == '__main__':
    app.run(debug=True)

I've tested the url '127.0.0.1:5000/test' by google's Postman.
I send the data to the url, and It worked, can return the result I wanted.

And I created another .py to test the url, too. It also can show the result I wanted.

import requests

payload = {'Message':'yo','Port':'123'}
r = requests.post("http://127.0.0.1:5000/test", data=payload)
print r.text

Then I put the same code below

@app.route('/', methods=['POST'])
def root():

I want to use the url '127.0.0.1:5000/' to send data to the '127.0.0.1:5000/test' (I also Use google's Postman)
And it can't worked... it always show 'loading...'

Is there a better way to implement my requirement ?
I will thank you so so so much ~!!

h8a2n5k23
  • 57
  • 1
  • 7

1 Answers1

-1

Instead of sending a local POST request, which would take a lot longer time, just have them both route to the same function, and set defaults.

@app.route('/', methods=['GET'])
@app.route('/test', methods=['POST'])
def test():
    return request.form.get('Message', 'yo') + ',' + request.form.get('Port', '123')

Which would respond normally to all POST requests on /test, and with a GET to /, it works the same as POSTing {'Message': 'yo', 'Port': '123'} to /test

Artyer
  • 31,034
  • 3
  • 47
  • 75