# URLSession and custom \`WWW-Authenticate\`

**URL:** <https://forums.swift.org/t/urlsession-and-custom-www-authenticate/73966>\
**Category:** Using Swift\
**Tags:** urlsession\
**Created:** [August 15, 2024, 12:05pm UTC](https://forums.swift.org/t/urlsession-and-custom-www-authenticate/73966 "2024-08-15T12:05:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nmgswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nmgswift/32/35189_2.png) [@nmgswift](https://forums.swift.org/u/nmgswift)\
**Post date:** [August 15, 2024, 12:05pm UTC](https://forums.swift.org/t/urlsession-and-custom-www-authenticate/73966/1 "2024-08-15T12:05:37Z")

</div>

Hello call!

I am attempting to use the `URLSession` API to call an endpoint with a custom [`WWW-Authenticate`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/WWW-Authenticate) scheme. For some reason, instead of allowing me to respond to this in the [challenge handler](https://developer.apple.com/documentation/foundation/urlsessiondelegate/1409308-urlsession) function of my delegate, it just immediately returns with a [NSURLErrorUserCancelledAuthentication](https://developer.apple.com/documentation/foundation/nsurlerrorusercancelledauthentication) error, which stops me from getting any `URLResponse`. I'm assuming something is going wrong internally.

Interestingly, if I use the legacy `NSURLConnection` API and forgo credentials altogether, I can actually get a usable `URLResponse` (albeit in the 400 range).

```swift
class MyConnectionDelegate: NSObject, NSURLConnectionDataDelegate {
  func connection(_ connection: NSURLConnection, didReceive response: URLResponse) {
    print("didReceive response")
    print(response)
  }
  func connectionShouldUseCredentialStorage(_ connection: NSURLConnection) -> Bool {
    return false
  }
}

let request = NSURLRequest(
  url: URL(string: "[XXX]")!)

let connection = NSURLConnection(
  request: request as URLRequest,
  delegate: MyConnectionDelegate()
)
connection?.start()
RunLoop.main.run()

```

This is obviously not preferable. Is there a way to do this with the `URLSession` API? Or at least see what's going wrong internally?

---

<div class="post-metadata">

**Author:** ![nmgswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nmgswift/32/35189_2.png) [@nmgswift](https://forums.swift.org/u/nmgswift)\
**Post date:** [August 15, 2024, 12:47pm UTC](https://forums.swift.org/t/urlsession-and-custom-www-authenticate/73966/2 "2024-08-15T12:47:09Z")

</div>

OK. I figured it out. I just have to set the `Authorization` header manually.

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [August 15, 2024, 2:39pm UTC](https://forums.swift.org/t/urlsession-and-custom-www-authenticate/73966/3 "2024-08-15T14:39:58Z")

</div>

FYI, `urlSession(_:didReceive:completionHandler:)` is only called when the server actually provides a authorization challenge of some kind, such as the `WWW-Authenticate: Basic` expected when using HTTP Basic, but only if the server actually replies with such a challenge. Generally `URLSession` doesn't consider response codes when calling various delegates, so the authentication challenge only happens for a response containing one of the supported challenge types, like basic or digest. (The same delegate is also reused for things like TLS verification, which can be rather confusing.)
