Search code examples
asp.net-corewebsockethttp-status-codes

Status code to indicate request is not a web Socket connection establishment request


Hi I am writing an end point in ASP.Net Core to handle web socket requests. Any idea what is the correct or most suitable response status code that i should return if i get a request which is not a websocket connection establishment request to a web socket connection end point.

For more info please see the below code,

 public async Task Invoke(HttpContext context)
    {
        if (!context.WebSockets.IsWebSocketRequest)
        {
            context.Response.StatusCode = <what should be set here>;
            return;
        }

Solution

  • Hard to say, but probably the best is 400 Bad Request. It's a general catch-all for anything the client sending being wrong in some way. It's traditionally used for validation, but the definition extends beyond that. If there's an endpoint for creating cars and you send it a bicycle, that's a bad request.