kkarol
kkarol

Reputation: 55

Custom headers - Alamofire + Swift 3

I'm getting an error while trying to use this code:

func getRawJSON(method: String, paramether: String) {
    let publicKey = "publicKeyHere"
    let secretKey = "secretKeyHere
    let APIURL = "https://www.bitmarket.pl/api2/"
    let time = String(Int(NSDate().timeIntervalSince1970))

    let query = NSURLComponents()
    query.queryItems = [NSURLQueryItem(name: "method", value: method) as URLQueryItem,
                        NSURLQueryItem(name: "tonce", value: time) as URLQueryItem]

    let requestString = query.query!
    let requestData = Array(requestString.utf8)

    let params = [
        "method": method,
        "tonce:": time
        ]

    let hmac: Array<UInt8> = try! HMAC(key: secretKey.utf8.map({$0}), variant: .sha512).authenticate(requestData)

    let hmacString = hmac.map{String(format: "%02X", $0)}.joined(separator: "").lowercased()

    let URL = NSURL(string: APIURL)!
    let mutableURLRequest = NSMutableURLRequest(url: URL as URL)
    mutableURLRequest.httpMethod = "POST"

    do {
        mutableURLRequest.httpBody = try JSONSerialization.data(withJSONObject: paramether, options: JSONSerialization.WritingOptions())
    } catch {
    }

    mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
    mutableURLRequest.setValue(publicKey, forHTTPHeaderField: "API-Key")
    mutableURLRequest.setValue(hmacString, forHTTPHeaderField: "API-Hash")

    Alamofire.request(mutableURLRequest) //Here is a problem

}

Here is the error:

Argument type 'NSMutableURLRequest' does not conform to expected type 'URLRequestConvertible'

What am I doing wrong? Alamofire documentation says NSMutableURLRequest could conform to URLRequestConvertible.

Upvotes: 1

Views: 565

Answers (1)

avinash pandey
avinash pandey

Reputation: 1381

Swift 3 defines URLRequest which conforms to the protocol URLRequestConvertible. You should use URLRequest instead of NSMutableURLRequest.

Refer to this discussion.

Upvotes: 2

Related Questions