3

I have an API method deploy on my local server,

@app.post("/test/api")
async def method():
     if incoming.request.url or domain == "this":
         do some operation
     else:
         skip it
     .....
     return something

Now, Few people are using my API method, but is there any way I could track who is calling my api method and do specific extra operations to the once I specified who is calling my api.

How can I track the incoming domain name or ip or url of the people who are using my api method?

Note: Need a basic example on how to acheive it if you familiar with it

Is it something possible?

user_12
  • 1,778
  • 7
  • 31
  • 72

2 Answers2

1

If you were using flask it could happen simply by getting remote_addr as below:

from flask import FLASK, request
@app.route('/test/api', methods=['POST']):
def method():
    visitor_ip = request.remote_addr

In fastapi it should happen like this: request.client.host

any way you can work with headers in your code and get many details of your client

0

If you can access incoming request headers, then check if X-Forwarded-For has IP address of client. If not it is possible that changing configuration of your setup will make it works as intended, however I have not experience with neither fastapi or uvicorn, so I am unable to write anything more precise.

Daweo
  • 31,313
  • 3
  • 12
  • 25