1

According to basic authentication schema, the http header should be format Authentication: Basic name:password to send with the url to server side.

Suppose I have a simple login function.

func login() {
    guard let url = URL(string: "http://0.0.0.0:5000/v1/token") else {
        print("Invalid url")
        return
    }

    var request = URLRequest(url: url)
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")

    // What's the exactly step to setup the basic authentication
    // request.httpMethod = "PUT"
    // request.addValue("Basic \(name):\(pass)", forHTTPHeaderField: "Authorization")

    URLSession.shared.dataTask(with: request) { data, response, error in
        if let data = data {
            if let decodeResponse = try? JSONDecoder().decode(Token.self, from: data) {
                DispatchQueue.main.async {
                    self.token = decodeResponse.token
                }

                return
            }
        }

        print("Fetch failed")
    }
}

How should I setup the basic authentication step?

ccd
  • 5,788
  • 10
  • 46
  • 96

0 Answers0