2016-08-09 65 views
2

我已捲曲請求構造爲這樣:夫特轉換捲曲-user鍵JSON爲AFNetworking

curl -X POST http://host.com \ 
    --data token=myToken \ 
    --data foo=myFoo \ 
    --user myUsername:myPassword 

我組織數據參數:

let params = ["token" : myToken, "foo" : myFoo] 

然後POST使用AFNetworking。

manager.POST("http://host.com", parameters: params, progress: nil, success: { task, responseObject in 
     //stuff 
     }, failure: { operation, error in 
     //error stuff  
    }) 

但我怎麼從捲曲--user關鍵,這一要求增加必要的參數?

回答

1

據悉,-u--user與基本身份驗證相同。您可以像這樣通過授權標頭傳遞用戶名和密碼,其中base64Encode()是我自己的字符串擴展名,可以轉換爲base64。

的完全轉換:

curl -X POST http://host.com \ 
--data token=myToken \ 
--data foo=myFoo \ 
--user myUsername:myPassword 

會像這樣:

let params = ["token" : myToken, "foo" : myFoo] 

if let base64 = "myUsername:myPassword".base64Encode() { 

    manager.requestSerializer.setValue("Basic " + base64, forHTTPHeaderField: "Authorization") 

    manager.POST("http://host.com", parameters: params, progress: nil, success: { task, responseObject in 
     //stuff 
     }, failure: { operation, error in 
      //error stuff 
    }) 
}