StompClientLib: Can't connect to websocket with authorization header

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

let request = NSURLRequest(url: url)
socketClient.openSocketWithURLRequest(request: request, delegate: self, connectionHeaders:["Authorization": "Bearer \(token)"])

Response:

received bad response code from server 401

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

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 21 (8 by maintainers)

Most upvoted comments

@cedb777 @cleverClosure I also can not to send headers through connectionHeaders, but I send it in request, like this

    var socketRequest: NSURLRequest {
        let webSocket = URL(string: urlString)!
        var request = URLRequest(url: webSocket)
            request.setValue(("Bearer " + token).toBase64, forHTTPHeaderField: "Authorization")
        return request as NSURLRequest
    }

@cedb777 @cleverClosure I also can not to send headers through connectionHeaders, but I send it in request, like this

    var socketRequest: NSURLRequest {
        let webSocket = URL(string: urlString)!
        var request = URLRequest(url: webSocket)
            request.setValue(("Bearer " + token).toBase64, forHTTPHeaderField: "Authorization")
        return request as NSURLRequest
    }

For the ones who could not make it using headers on connection, try this, that approach works on my environment.

It worked for me with this code: ` let url = URL(string: “wss://(defaultSocketBase)/push?access_token=(token)”)!

    let request = NSURLRequest(url: url)

    let headers: [String: String] = ["heart-beat": "20,20"]

    socketClient.openSocketWithURLRequest(request: request, delegate: self, connectionHeaders: headers)`