简体   繁体   中英

API call from another API python

I create two apps say: App1 and App2 with flask.

App1

@App1.route('/api/v1.0/call_database')

def _database():

 ...
 ...

App2

@App2.route('/api/v1.0/calculate')

def _calculate():
   ...
   ...

App1 is centrally contacting my database. How can I use App2 to call App1 ?

What I was trying is:

@App2.route('/api/v1.0/calculate')
def _calculate():
 ...
      response = requests.get(url = ('http://{}:{}/api/v1.0/call_database'.format(data_store_url, data_store_port)), data = parameters)

...

This is your first app script:

from flask import Flask, request

app = Flask(__name__)


@app.route("/app1/")
def app1():
    return str(request.args)


app.run(port=5000)

This is your second app script:

from flask import Flask, request
import requests

app = Flask(__name__)


@app.route("/app2/")
def app2():
    # requests.get(url, params={})
    res = requests.get("http://127.0.0.1:5000/app1/", params={"a": "123"})
    return str(res.text)


app.run(port=5001)

When you go to http://127.0.0.1:5001/app2/ - you get:

ImmutableMultiDict([('a', u'123')])

That's expected. That's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM