2016-09-18 55 views
1

這是我的輸入如何使用Almofire發送http post請求與http頭和驗證?

1.parameters

let parameters: Parameters = ["username ": "Henry","password":"xxxx","key"="ewq2356"] 

2.http頭

let headers = [ "Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==", "Content-Type": "application/x-www-form-urlencoded" ] 

3.Authentication與URLCredential

let user = "user1" let password = "password1" 

4.URL

http://myapi.com/rest/index.php 

那麼我怎麼能使用它使用Almofire http post方法?

回答

2

示例API - >

func someFunction() 
{ 

    Alamofire.request(.POST, "apiName", 
     parameters:["Key":"Value"], 
     headers: ["Content-type application":"json"]) 
     .authenticate(user: "userName", password: "Password") 
     .response { request, response, data, error in 
      if error == nil { 
       print(request) 
       print(response) 
      } 
      else { 
       //Display Error Message 
       print(error) 
      } 

    } 
} 
+0

OK。所以,當我通過把參數表單數據的身體和頭部的頭域之前,它要求我所提到的URL untentication登錄後在人檢查它在我與URLCredential 讓用戶=「USER1」問題認證讓密碼=「密碼1」 – Bhagabata

+0

Almofire說HTTP基本驗證 上一個請求會自動提供URLCredential以在適當的時候一個URLAuthenticationChallenge的身份驗證方法: 讓用戶= 「U ser「 let password =」password「 Alamofire.request(」https://httpbin.org/basic-auth/\(user)/\(password)「) .authenticate(user:user,password:password ) .responseJSON { debugPrint(響應) }的響應 – Bhagabata

+0

是用戶名和密碼是HTTP Auth的一部分,還是簡單的參數? –