master.I am following the Fastapi websockets tutorial (https://fastapi.tiangolo.com/advanced/websockets/#await-for-messages-and-send-messages). With the exact code given (see link) I start uvicorn with uvicorn main:app --reload visit http://127.0.0.1:8000/. I get this error in my chrome console:
(index):16 WebSocket connection to 'ws://localhost:8000/ws' failed: Error during WebSocket handshake: Unexpected response code: 400
When I click "send" I expect to see the message I typed appear on the screen
When I click "send" nothing happens
OS / Python / Uvicorn version: just run uvicorn --version
macos 10.15.5
python 3.8.2
uvicorn 0.12.1
The exact command you're running uvicorn with, all flags you passed included. If you run it with gunicorn please do the same. If there is a reverse-proxy involved and you cannot reproduce without it please give the minimal config of it to reproduce.
uvicorn main:app --reload
I just tested the code you linked and it's working fine for me, you got no logs ?
❯ uvicorn fapiws:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [8960] using watchgod
INFO: Started server process [8970]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:43636 - "GET / HTTP/1.1" 200 OK
INFO: ('127.0.0.1', 43640) - "WebSocket /ws" [accepted]
Not when running with the above command. Here it is with log level trace
(.venv) ➜ websocket_test uvicorn main:app --reload --log-level trace
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [4573] using statreload
INFO: Started server process [4575]
INFO: Waiting for application startup.
TRACE: ASGI [1] Started scope={'type': 'lifespan', 'asgi': {'version': '3.0', 'spec_version': '2.0'}}
TRACE: ASGI [1] Receive {'type': 'lifespan.startup'}
TRACE: ASGI [1] Send {'type': 'lifespan.startup.complete'}
INFO: Application startup complete.
TRACE: 127.0.0.1:50372 - Connection made
TRACE: 127.0.0.1:50373 - Connection made
TRACE: 127.0.0.1:50372 - ASGI [2] Started scope={'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('127.0.0.1', 8000), 'client': ('127.0.0.1', 50372), 'scheme': 'http', 'method': 'GET', 'root_path': '', 'path': '/', 'raw_path': b'/', 'query_string': b'', 'headers': '<...>'}
TRACE: 127.0.0.1:50372 - ASGI [2] Send {'type': 'http.response.start', 'status': 200, 'headers': '<...>'}
INFO: 127.0.0.1:50372 - "GET / HTTP/1.1" 200 OK
TRACE: 127.0.0.1:50372 - ASGI [2] Send {'type': 'http.response.body', 'body': '<1025 bytes>'}
TRACE: 127.0.0.1:50372 - ASGI [2] Completed
TRACE: 127.0.0.1:50375 - Connection made
WARNING: Unsupported upgrade request.
TRACE: 127.0.0.1:50375 - Connection lost
TRACE: 127.0.0.1:50372 - ASGI [3] Started scope={'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('127.0.0.1', 8000), 'client': ('127.0.0.1', 50372), 'scheme': 'http', 'method': 'GET', 'root_path': '', 'path': '/', 'raw_path': b'/', 'query_string': b'', 'headers': '<...>'}
TRACE: 127.0.0.1:50372 - ASGI [3] Send {'type': 'http.response.start', 'status': 200, 'headers': '<...>'}
INFO: 127.0.0.1:50372 - "GET / HTTP/1.1" 200 OK
TRACE: 127.0.0.1:50372 - ASGI [3] Send {'type': 'http.response.body', 'body': '<1025 bytes>'}
TRACE: 127.0.0.1:50372 - ASGI [3] Completed
TRACE: 127.0.0.1:50372 - Connection lost
I fail to see what could cause this, do you use h11 or websockets ? Any particular browser used ?
I get the same error with exactly the same setup. Tested with Crome and Safari.
Fixed it with:
pip3 install websockets
ok the new packaging does not install websocket or h11 by default so with a simple pip install uvicorn no ws will be handled.
to fix this. install with pip install uvicorn[standard].
we need to either provide a gentle message on ws usage if the latter installation was used or update docs, or both
In zsh on macos 10.15.5 i got the error
zsh: no matches found: uvicorn[standard]
I had to escape the square brackets:
pip3 install uvicorn\[standard\]
In zsh on macos 10.15.5 i got the error
zsh: no matches found: uvicorn[standard]I had to escape the square brackets:
pip3 install uvicorn\[standard\]
this is a zsh feature yes http://zsh.sourceforge.net/Guide/zshguide05.html#l137
usually you'd better quoting the brackets rather than escaping them
@euri10 yes, you should provide a gentle message...
Definitely should provide a gentle message...
Most helpful comment
ok the new packaging does not install websocket or h11 by default so with a simple
pip install uvicornno ws will be handled.to fix this. install with
pip install uvicorn[standard].we need to either provide a gentle message on ws usage if the latter installation was used or update docs, or both