title: Apollo iOS GraphQL Subscription Error: "Invalid HTTP upgrade"
body: <p>I'm trying to get a GraphQL subscription from SoundtrackYourBrand working within my iOS app. The queries/mutations work fine but when I use the web socket I get either error 400 or 403 (&quot;Invalid HTTP upgrade&quot;) or 1011 (no message) depending on how I've set the authorisation token.</p>
  <blockquote>
  <p>Error WebSocketError(payload: nil, error: Optional(Starscream.WSError(type: Starscream.ErrorType.upgradeError, message: &quot;Invalid HTTP upgrade&quot;, code: 403)), kind: ApolloWebSocket.WebSocketError.ErrorKind.networkError)</p>
  </blockquote>
  <blockquote>
  <p>Error WebSocketError(payload: nil, error: Optional(Starscream.WSError(type: Starscream.ErrorType.protocolError, message: &quot;&quot;, code: 1011)), kind: ApolloWebSocket.WebSocketError.ErrorKind.networkError)</p>
  </blockquote>
  <p>I'm using Apollo to work with the graphQL API. And it works for mutations/queries. I've set up a splitNetwork Transport as per the Apollo docs <a href="https://www.apollographql.com/docs/ios/subscriptions/" rel="nofollow noreferrer">here</a>.</p>
  <pre><code>var wssURL: String = &quot;wss://api.soundtrackyourbrand.com/v2&quot;
  
      client = {
          let request = URLRequest(url: wssURL)
          let authPayload = [&quot;Authorization&quot; : &quot;Bearer \(token)&quot;]
          let webSocketTransport = WebSocketTransport(request: request, connectingPayload: authPayload)
          
          let client = URLSessionClient()
          let cache = InMemoryNormalizedCache()
          let store = ApolloStore(cache: cache)
          let provider = NetworkInterceptorProvider(store: store, client: client)
          let normalTransport = RequestChainNetworkTransport(interceptorProvider: provider, endpointURL: endpointURL, additionalHeaders: [&quot;authorization&quot; : &quot;Bearer \(token)&quot;])
          
          let splitNetworkTransport = SplitNetworkTransport(
              uploadingNetworkTransport: normalTransport,
              webSocketNetworkTransport: webSocketTransport
          )
          
          return ApolloClient(networkTransport: splitNetworkTransport, store: store)
          
      }()
  </code></pre>
  <p>While above is the way Apollo suggests doing authorisation, in the SoundtrackYourbrand docs it says set the token as a query parameter: <a href="https://developer.soundtrackyourbrand.com/api/#subscriptions" rel="nofollow noreferrer">&quot;You won't be using any headers, so the token need to be passed as a query parameter.&quot;</a></p>
  <p>In which case should I set up the websocket like below.</p>
  <p>The below code gives me the protocol error 1011, while the above code gives the 400/403 &quot;Invalid HTTP upgrade&quot; error.</p>
  <pre><code>    client = {
          
          let queryItems = [URLQueryItem(name: &quot;Authorization&quot;, value: &quot;Bearer \(token)&quot;)]
          var urlComps = URLComponents(string: wssURL)!
          urlComps.queryItems = queryItems
          let result = urlComps.url!
          let request = URLRequest(url: result)
          let webSocketTransport = WebSocketTransport(request: request)
          
          let client = URLSessionClient()
          let cache = InMemoryNormalizedCache()
          let store = ApolloStore(cache: cache)
          let provider = NetworkInterceptorProvider(store: store, client: client)
          let normalTransport = RequestChainNetworkTransport(interceptorProvider: provider, endpointURL: endpointURL, additionalHeaders: [&quot;authorization&quot; : &quot;Bearer \(token)&quot;])
          
          let splitNetworkTransport = SplitNetworkTransport(
              uploadingNetworkTransport: normalTransport,
              webSocketNetworkTransport: webSocketTransport
          )
          
          return ApolloClient(networkTransport: splitNetworkTransport, store: store)
          
      }()
  </code></pre>
  <p>Also should my starting URL be:</p>
  <ol>
  <li>wss://api.soundtrackyourbrand.com/v2</li>
  <li>wss://api.soundtrackyourbrand.com/v2/websocket</li>
  </ol>
  <p>Looking at the docs I would think it should be 2), but after getting a subscription working in the SoundtrackYourbrand <a href="https://api.soundtrackyourbrand.com/v2/explore" rel="nofollow noreferrer">workspace</a>, which uses 1) I assume I must use 1).</p>
  <p>In the workspace I use the WS URL provided by default: wss://api.soundtrackyourbrand.com/v2, and then set the authorisation header to &quot;Bearer ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••...&quot;. And that works, but can't seem to replicate it in the iOS app with Apollo</p>
  <p>What am I doing wrong?</p>
  <p>Any help would be greatly appreciated!</p>