Reputation: 1006
I am using uvicorn as server to run app using fast api. While executing endpoint url in Swagger, following message is shown in response header of server response.
content-length: 122
content-type: application/json
date: Sat12 Dec 2020 10:18:55 GMT
server: uvicorn
How to change server name to new name as server : firstproject? Following code concatenates server name unciorn with new name
@app.middleware("http")
async def add_custom_header(request, call_next):
response = await call_next(request)
response.headers['server'] = 'firstproject'
return response
This gives the following output
content-length: 122
content-type: application/json
date: Sat12 Dec 2020 10:19:33 GMT
server: uvicornfirstproject
How to change server name to server : firstproject in response header?
EDIT
In start_server.py
import uvicorn
from app.main import app
if __name__ == "__main__":
uvicorn.run("start_server:app --header server:firstproject", host="0.0.0.0", port=8000, reload=True)
gives following error
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO: Started reloader process [15256] using statreload
ERROR: Error loading ASGI app. Attribute "app --header server:firstproject" not found in module "start_server".
I run the code from Visual studio
Upvotes: 6
Views: 5487
Reputation: 597
In case of you need to "delete" the "server" header, you can use option --no-server-header
uvicorn my_app:app --no-server-header
If you are running uvicorn from a python file:
if __name__ == '__main__':
uvicorn.run('my_app:app', server_header=False)
Upvotes: 8
Reputation: 20608
You can set a custom header when running uvicorn.
--header TEXT
Specify custom default HTTP response headers as a Name:Value pair
When you run it like this, it will override the default server name.
uvicorn my_app:app --header server:firstproject
If you are running uvicorn from a python file. You need to pass them as tuple inside a list.
if __name__ == "__main__":
uvicorn.run("my_app:app", headers=[("server", "firstproject")])
Upvotes: 12