Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't connect to websocket : received bad response code from server 422 #80

Closed
tomasimartin opened this issue Dec 3, 2019 · 12 comments
Closed

Comments

@tomasimartin
Copy link

tomasimartin commented Dec 3, 2019

Hi. I get an error while trying to connect to websocket, here is the code:

if let url = NSURL(string: hostws) {
            socketClient.openSocketWithURLRequest(request: NSURLRequest(url: url as URL), delegate: self)
        }

Response:

Error Domain=SRWebSocketErrorDomain Code=2132 "received bad response code from server 422" UserInfo={NSLocalizedDescription=received bad response code from server 422, HTTPResponseStatusCode=422}

And if I use another "Starscream" library, it connects very well with the same url, but I need the "STOMP" part that this other library doesn't have

Do you have any ideas why that might be the case?

@WrathChaos
Copy link
Owner

Hello @tomasimartin,
Thank you for using the StompClientLib. Actually it is a problem with your backend side.

Here is What Is 422 Unprocessable Entity? article about the bad response. Please contact with your backend devs :) However, I'm also here to help, discuss your problem :)
Have fun!

@tomasimartin
Copy link
Author

hello @WrathChaos
The same API is used in Kotlin (Android) and also in Java, on the same url. It works perfectly on both environments.

As I said in my previous message, I used another library to make websocket, and with the same url on that library it works.

That's why I'm focusing my research here. Has anyone ever encountered this problem?

@WrathChaos
Copy link
Owner

Actually there are some similar issues. Some related issues:
#30
#39
#68

I know that it could work on Android however, iOS requires so many different things to do :( I believe that you need to research about it. Currently, it works with my example project & other recent projects. Have you ever tried with simple example project?

@tomasimartin
Copy link
Author

@WrathChaos thanks for your quick feedback, we found the problem internally, it was a server side headers check. I hope I didn't disturb you too much. Well, hello from France. you can close this issue

@WrathChaos
Copy link
Owner

@tomasimartin I'm glad to hear that :) You're always welcome to come & brainstorm :) Please feel free to ask anything :)

@tomasimartin
Copy link
Author

Hi @WrathChaos ,

we found out where the problem came from, when connecting there is an http call where we pass in the header

HEADER={{"host":["dev.myserver.fr"], "sec-websocket-protocol":["], "sec-websocket-key":["M6rALuZT9o3NRROXksQ==="], "sec-websocket-version":["13"], "upgrade":["websocket"], "origin":["http://dev.myserver.fr], "connection":["Upgrade"]}

but I don't find in this header the parameters I added during the connection:

let headerWS = ["deviceId":deviceToken]
socketClient.openSocketWithURLRequest(request: NSURLRequest(url: url as URL), delegate: self, connectionHeaders: headerWS)

it can come from the SocketRocket library, the initial call to the connection is made without taking into account the header.

I'm not good enough in objective-c to modify the lib myself.

Do you have an idea to pass my header ["deviceId":deviceToken], during the initial call

@WrathChaos
Copy link
Owner

Hey @tomasimartin,
I'm gonna try it with modifying SocketRocket but I need some time to test it. ObjC is really a pain :D

@tomasimartin
Copy link
Author

To get around my problem, I passed the header differently, like this

if let url = NSURL(string: hostws) {
            var deviceTokenManager = DeviceTokenManager()
            if let deviceToken = deviceTokenManager.retreiveToken() {
                let headerWS = ["deviceId":deviceToken]
                let request = NSMutableURLRequest(url: url as URL)
                request.setValue(deviceToken, forHTTPHeaderField: "deviceId")
                socketClient.openSocketWithURLRequest(request: request, delegate: self, connectionHeaders: headerWS)
            }
        }

@bejeri4
Copy link

bejeri4 commented Mar 11, 2020

@tomasimartin
Hello. I have got the same problem.

Error Domain=SRWebSocketErrorDomain Code=2132 "received bad response code from server 403" UserInfo={NSLocalizedDescription=received bad response code from server 403, HTTPResponseStatusCode=403}

As you said there is a server side problem (checking headers). Could you please give me more detailed information.

Thanks in advance!

@tusharMistri
Copy link

@WrathChaos , Can you please tell me what is reason behind this error.

Error Domain=SRWebSocketErrorDomain Code=2132 "received bad response code from server 403" UserInfo={NSLocalizedDescription=received bad response code from server 403, HTTPResponseStatusCode=403}

I have checked with backend server, and also verify with Android its also working fine, when i am trying to connect socket from iOS getting error.

I have tried all cases like as,
URL : ws://{server_url}/ws/websocket
URL : wss://{server_url}/ws/websocket
URL : http://{server_url}/ws/websocket
URL : https://{server_url}/ws/websocket

But i am not getting result. can you please explain where i am wrong and missing something from my end ?

Thanks in advance.

@KaranMehta3984
Copy link

@tusharMistri
Any luck?

@AlexandreCassagne
Copy link

AlexandreCassagne commented Jan 5, 2024

why was this issue marked as closed?

I am getting the same issue.

EDIT: Never mind :D Unrelated issue. Cors settings are not shared between HTTP and Stomp/WS in Spring (my backend)

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/websocket")
                .setAllowedOriginPatterns("*");
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants