I take the IP address using client.host
from the Request
object and send this to another function, where I'm using Pydantic's IPvAnyAddress
to validate the IP address.
Here is my code:
from fastapi import APIRouter, Request
from pydantic import IPvAnyAddress
route = APIRouter()
@route.get("/ip-address")
def request_ip_address_deblock_link(request: Request):
return example_function(request.client.host)
def example_function(ip_address: IPvAnyAddress):
print(ip_address)
But when I'm using FastAPI's TestClient
to test my API routes, the IP-address check fails, as the hostname in the request is testclient
.
ValueError: 'testclient' does not appear to be an IPv4 or IPv6 address
Is it possible to change the hostname in FastAPI/Starlette's TestClient
?